If you’re working with Python for the first time, you might be interested in learning how to store data. For example, you might want to collect a list of exam scores, so you can average them. If so, the list data structure gets the job done. But, how do you create a list in Python? That’s the topic of today’s article.
As it turns out, there are a few different ways to create a list. First, we could create a list directly as follows:
my_list = [0, 1, 2]. Alternatively, we could build that same list using a list comprehension:
my_list = [i for in range(0, 3)]. Finally, if we need more control, we could build up our list using a loop and
append(). In the remainder of this article, we’ll look at each solution in detail.
Table of Contents
When it comes to working with different types of data in Python, it’s helpful to have some way to manage it. Luckily, Python supports and easy-to-use data structure for storing all kinds of data: the list.
In Python, the list is an array-like data structure which is dynamic in size. In other words, we don’t have to worry about knowing how many items we have before we create our list. For those of us who work in languages like Java or C, we’re used to being stuck with the following syntax:
int list = new int;
Luckily, Python has a much cleaner syntax. Specifically, we have two options:
- Create a list using the constructor:
my_list = list()
- Or, create a list using an empty list:
my_list = 
But, what if we want to populate that list? That’s the problem we’ll be tackling today. Fortunately, there are several solutions.
At this point, we’ll take a look at a few ways to create a list in Python. As always, we’ll work our way from straightforward to more complex solutions. After that, we’ll compare each solution’s performance. At any rate, let’s dive in!
Create a List by Hand
One of the nice things about Python is that we can create a list by hand. In other words, if we know how we want to populate the list, we can write those contents out directly:
my_list = ["Crosby", "Malkin", "Letang", "Rust"]
In one line, we managed to create a variable called
my_list. Then, we assigned it a literal list which contains a few Penguins players.
Now, if we want to interact with this list, we can. For instance, we could get any of the following information:
- First player:
- Last player:
- First two players:
- Every other player:
If you’re interested in articles on list interaction, I’ve written a thing or two:
- How to Get the Last Item of a List in Python
- How to Check if a List is Empty in Python
- How to Copy a List in Python
Otherwise, let’s look at a few other solutions.
Create a List with a Loop
Since lists in Python are dynamic, we don’t actually have to define them by hand. In other words, we can create an empty list and add items to it with a loop:
my_list =  for i in range(10): my_list.append(i)
Here, we’ve created an empty list and assigned it to
my_list. Then, we run a loop over a range of numbers between 0 and 9. Each number is then added to the list using
When this code snippet is all said and done, we’ll have a list that looks something like the following:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
As we can probably image, we could use this loop to populate a list in just about any way we want. For example, we could modify our range to give us only even values:
my_list =  for i in range(0, 10, 2): my_list.append(i)
In this case,
my_list would only contain even numbers between 0 and 9:
[0, 2, 4, 6, 8]
Likewise, there are a ton of different ways to add items to lists as well. If you’re interested in that sort of thing, I have an article on that.
Create a List with a List Comprehension
One of my favorite ways to create a list is using the list comprehension functionality. Essentially, it’s a compressed loop syntax that lets us generate simple lists. For instance, the first loop example could be written as a list comprehension as follows:
my_list = [i for i in range(10)]
Now, instead of manually appending items to a list, this expression handles all the heavy lifting. As a result, we can focus on modifying the expression to do fun things like scale all values in the range by 3:
my_list = [i * 3 for i in range(10)]
This expression will generate a list that looks like the following:
[0, 3, 6, 9, 12, 15, 18, 21, 24, 27]
Now that we have a few solutions, let’s compare their performance. To do that, we’ll need to generate some strings:
static = """ my_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] """ loop = """ my_list =  for i in range(10): my_list.append(i) """ comprehension = """ my_list = [i for i in range(10)] """
At this point, all we have to do is import the
timeit library, so we can begin testing:
>>> import timeit >>> min(timeit.repeat(stmt=static)) 0.08311530000000289 >>> min(timeit.repeat(stmt=loop)) 1.0872243000000026 >>> min(timeit.repeat(stmt=comprehension)) 0.7429419999999993
And, there you have it! The quickest way to create a list is to declare it statically. That said, if you have to generate a list, the list comprehension seems to be the way to go.
Now that you know how to create a list, I have a little challenge for you: create a list which contains the first 100 terms of the fibonacci sequence. For the purposes of this exercise, we’ll assume the first two terms are 1 and 1.
Feel free to use any solution from this article to generate your list. For example, you could compute the first 100 terms by hand and build up your list statically, Alternatively, you might choose to use a loop or even recursion to populate your list.
If you manage to generate the list using a list comprehension, let me know! I have no idea if that’s possible–at least not without doing some really nasty stuff. After all, you can’t access elements of the list as you’re building it, so it would be tough to track the previous values. That said, you may be able to take advantage of the new walrus operator or some outside state tracking mechanism.
To kick things off, here’s a solution by our friend, Muhimen:
If you come up with something different, feel free to post it on Twitter using the #RenegadePython hashtag! If I see it, I’ll be sure to give it a share.
A Little Recap
At this point, we’ve reached the end of the article. As always, I like to share a list of all the solutions for your perusal:
# Create a list statically my_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] # Create a list with a loop my_list =  for i in range(0, 10, 2): my_list.append(i) # Create a list with a list comprehension my_list = [i for i in range(10)]
And with that, all I ask is that you take a trip over to my post about different ways to support the site. If you could chip in, that would really help the site grow. Otherwise, check out some of these Python resources on Amazon (ad):
- Python for Beginners: 2 Books in 1: Python Programming for Beginners, Python Workbook
- A Smarter Way to Learn Python: Learn it faster. Remember it longer.
While you’re here, consider checking out some of these related articles:
Thanks for sticking around! I appreciate it.
The backbone of any Python program is decision making. In other words, given some input, what should our program do? Today, we'll answer part of that by learning if statements.
The first step in opening a jar is to get a jar. But, how do we do that? Let's let recursion figure that out for us.