Comprehensions and Assignment Expressions
Learn how to make your code more concise using comprehension expressions.
Comprehension expressions are usually a more concise way of writing code, and in general, code written this way tends to be easier to read. If we need to do some transformations on the data we're collecting, using a comprehension might lead to more complicated code. In these cases, writing a simple for
loop should be preferred instead.
There is, however, one last resort we could apply to try to salvage the situation: assignment expressions. In this lesson, we discuss these alternatives.
Usage of comprehension expressions
The use of comprehensions is recommended for creating data structures in a single instruction, instead of multiple operations. For example, if we wanted to create a list with calculations over some numbers in it, we might try writing it like this:
Get hands-on with 1400+ tech skills courses.