How to Create Lists in Python
00:00
One way to create lists in Python is using loops, and the most common type of loop is the for
loop. You can use a for
loop to create a list of elements in three steps.
00:10 Step 1 is instantiate an empty list, step 2 is loop over an iterable or range of elements, and step 3 is to append each element to the end of the list.
00:21
If you want to create a list containing the first ten perfect squares, then you can complete these steps in three lines of code. Here, you instantiate an empty list called squares
.
00:33
Then you use a for
loop to iterate over the range(10)
. Finally, you multiply each number by itself and append the result to the end of the list.
00:48
map()
objects provide an alternative approach that’s based in functional programming. By the way, you can learn all about functional programming in Python in another course here at Real Python.
00:59
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 element 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, get_price_with_tax
.
01:24
You pass both of these arguments to map()
and store the resulting object in final_prices
. You can easily convert this map()
object into a list using list()
.
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.
02:10
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.
02:28
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 range(10)
.
02:48
A list comprehension in Python works well in many places where you would use map()
.
02:54
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 map()
object.
03:10
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.
03:43
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.
Martin Breuss RP Team on Aug. 9, 2024
@Ross good point, thanks for the feedback.
It’s a side-step and definitely not necessary, but if you (or anyone else reading this) is interested to learn more about map()
, then you can check out Python’s map(): Processing Iterables Without a Loop.
Become a Member to join the conversation.
Ross on Aug. 8, 2024
I was confused by the map() discussion. I think that could be left for a later lesson that covers it more thoroughly.