How to Create Lists in Python
You pass in a function and an iterable, and
map() will create an object. This object contains the output that you would get from running each iterable elements through the supplied function. As an example, consider a situation in which you need to calculate the price after tax for a list of transactions. Here you have an iterable,
txns, and a function,
01:40 List comprehensions are a third way of making lists. Rather than creating an empty list and adding each element to the end, you simply define the list and its contents at the same time by following this format.
01:52 Every list comprehension in Python includes three elements: the expression, the member, and the iterable. I’ll explain each of these when we step through an example in a second. With this elegant approach, you could rewrite the loop from the last example in just a single line of code.
As I said, every list comprehension in Python includes three elements. Expression is the member itself—a call to a method or any other valid expression that returns a value. In this example, the expression
i * i is the square of the member value.
Member is the object or value in the list or iterable. In this example, the member value is
i. And iterable is a list, set, sequence, generator, or any other object that can return its elements one at a time. In this example, the iterable is
You can rewrite the pricing example with its own list comprehension. The only distinction between this implementation and
map() is that the list comprehension in Python returns a list, not a
List comprehensions are often described as being more Pythonic than loops or
map(), but rather than blindly accepting that assessment, it’s worth it to understand the benefits of using a list comprehension in Python when compared to the alternatives. Later on, you’ll learn about a few scenarios where the alternatives are actually a better choice.
03:29 One main benefit of using a list comprehension in Python is that it is a single tool that you can use in many different situations. In addition to standard list creation, list comprehensions can also be used for mapping and filtering.
You don’t have to use a different approach for each scenario. This is the main reason why list comprehensions are considered Pythonic, as Python embraces simple, powerful tools that you can use in a wide variety of situations. As an added side benefit, whenever you use a list comprehension in Python, you won’t need to remember the proper order of arguments like you would when you call
map(). List comprehensions are also more declarative than loops, which means they’re easier to read and understand. Loops require you to focus on how the list is created. You have to manually create an empty list, loop over the elements, and add each of them to the end of the list. With a list comprehension in Python, you can instead focus on what you want to do in the list and trust that Python will take care of how the list construction takes place.
04:35 Now that you understand some of the benefits of using list comprehensions, in the next lesson you’ll gain a more in-depth understanding of the full value that list comprehensions provide by exploring their range of possible functionality.
Become a Member to join the conversation.