List Comprehensions

Learn how to use list comprehension to create lists in a clear and declarative manner.

We'll cover the following

It is often useful to create a list with particular content, perhaps based on another list or iterable. It is possible to do this using a loop, or, perhaps, a map function.

List comprehensions provide an alternative that is more declarative than a loop and often clearer than using a map function.

In addition to list comprehensions, there are similar techniques for generating lazy iterators (generator comprehensions), sets, and dictionaries, which we will also cover in this chapter.

Get hands-on with 1200+ tech skills courses.