Python Lists

Lists allow you to store sets of information in one place, whether you have just a few items or millions of items. Lists are one of Python’s most powerful features readily accessible to new programmers, and they tie together many important concepts in programming.

What is a List?

A list is a collection of items in a particular order.

You can make a list that includes the letters of the alphabet, the digits from 0–9, or the names of all the people in your family. You can put anything you want into a list, and the items in your list don’t have to be related in any particular way. Because a list usually contains more than one element, it’s a good idea to make the name of your list plural, such as letters, digits, or names. In Python, square brackets [] indicate a list, and individual elements in the list are separated by commas.

names = ["Ahmed", "Mohammed", "Ali", "Omar"]
print(names)

If you ask Python to print a list, Python returns its representation of the list, including the square brackets.

['Ahmed', 'Mohamed', 'Ali', 'Omar']

Because this isn’t the output you want your users to see, let’s learn how to access the individual items in a list.

Accessing Elements in a List

Lists are ordered collections, so you can access any element in a list by telling Python the position, or index, of the item desired. To access an element in a list, write the name of the list followed by the index of the item enclosed in square brackets.

names = ["Ahmed", "Mohammed", "Ali", "Omar"]
print(names[0])
Ahmed

This is the result you want your users to see, clean, neatly formatted output. You can also use the string methods on any element in this list.

names = ["Ahmed", "Mohammed", "Ali", "Omar"]
print(names[0].lower())
ahmed

Index Positions Start at 0, Not 1

Python considers the first item in a list to be at position 0, not position 1. This is true of most programming languages, and the reason has to do with how the list operations are implemented at a lower level. If you’re receiving unexpected results, determine whether you are making a simple off-by-one error.

The second item in a list has an index of 1. Using this counting system, you can get any element you want from a list by subtracting one from its position in the list. For instance, to access the fourth item in a list, you request the item at index 3.

Python has a special syntax for accessing the last element in a list. By asking for the item at index -1, Python always returns the last item in the list.

names = ["Ahmed", "Mohammed", "Ali", "Omar"]
print(names[-1])
Omar

This syntax is quite useful, because you’ll often want to access the last items in a list without knowing exactly how long the list is. This convention extends to other negative index values as well. The index -2 returns the second item from the end of the list, the index -3 returns the third item from the end, and so forth.

Using Individual Values from a List

You can use individual values from a list just as you would any other variable. For example, you can use f-strings to create a message based on a value from a list.

names = ["Ahmed", "Mohammed", "Ali", "Omar"]
message = f"Hello, {names[0]}!"
print(message)
Hello, Ahmed!

Changing, Adding, and Removing Elements

Most lists you create will be dynamic, meaning you’ll build a list and then add and remove elements from it as your program runs its course. For example, you might create a game in which a player has to shoot aliens out of the sky. You could store the initial set of aliens in a list and then remove an alien from the list each time one is shot down. Each time a new alien appears on the screen, you add it to the list. Your list of aliens will increase and decrease in length throughout the course of the game.

Modifying Elements in a List

The syntax for modifying an element is similar to the syntax for accessing an element in a list. To change an element, use the name of the list followed by the index of the element you want to change, and then provide the new value you want that item to have.

names = ["Ahmed", "Mohammed", "Ali", "Omar"]
print(names)

names[0] = "Hassan"
print(names)
['Ahmed', 'Mohamed', 'Ali', 'Omar']
['Hassan', 'Mohamed', 'Ali', 'Omar']

Adding Elements to a List

You might want to add a new element to a list for many reasons. For example, you might want to make new aliens appear in a game, add new data to a visualization, or add new registered users to a website you’ve built. Python provides several ways to add new data to existing lists.

Appending Elements to the End of a List

The simplest way to add a new element to a list is to append the item to the list. When you append an item to a list, the new element is added to the end of the list.

names = ["Ahmed", "Mohamed", "Ali", "Omar"]
print(names)

names.append("Hassan")
print(names
['Ahmed', 'Mohamed', 'Ali', 'Omar']
['Ahmed', 'Mohamed', 'Ali', 'Omar', 'Hassan']

The append() method adds "Hassan" to the end of the list without affecting any of the other elements in the list. The append() method makes it easy to build lists dynamically. For example, you can start with an empty list and then add items to the list using a series of append() calls.

names = []

names.append("Ahmed")
names.append("Mohamed")
names.append("Ali")

print(names)
['Ahmed', 'Mohamed', 'Ali']

Building lists this way is very common, because you often won’t know the data your users want to store in a program until after the program is running. To put your users in control, start by defining an empty list that will hold the users’ values. Then append each new value provided to the list you just created.

Inserting Elements into a List

You can add a new element at any position in your list by using the insert() method. You do this by specifying the index of the new element and the value of the new item.

names = ["Mohamed", "Ali", "Omar"]
print(names)

names.insert(0, "Ahmed")
print(names)
['Mohamed', 'Ali', 'Omar']
['Ahmed', 'Mohamed', 'Ali', 'Omar']

The insert() method opens a space at position 0 and stores the value "Ahmed" at that location. This operation shifts every other value in the list one position to the right.

Removing Elements from a List

Often, you’ll want to remove an item or a set of items from a list. For example, when a player shoots down an alien from the sky, you’ll most likely want to remove it from the list of active aliens. Or when a user decides to cancel their account on a web application you created, you’ll want to remove that user from the list of active users. You can remove an item according to its position in the list or according to its value.

Removing an Item Using the del Statement

If you know the position of the item you want to remove from a list, you can use the del statement.

names = ["Ahmed", "Mohamed", "Ali", "Omar"]
print(names)

del names[0]
print(names)
['Ahmed', 'Mohamed', 'Ali', 'Omar']
['Mohamed', 'Ali', 'Omar']

You can remove an item from any position in a list using the del statement if you know its index.

names = ["Ahmed", "Mohamed", "Ali", "Omar"]
print(names)

del names[1]
print(names)
['Ahmed', 'Mohamed', 'Ali', 'Omar']
['Ahmed', 'Ali', 'Omar']

In both examples, you can no longer access the value that was removed from the list after the del statement is used.

Removing an Item Using the pop() Method

Sometimes you’ll want to use the value of an item after you remove it from a list. For example, you might want to get the x and y position of an alien that was just shot down, so you can draw an explosion at that position. In a web application, you might want to remove a user from a list of active members and then add that user to a list of inactive members.

The pop() method removes the last item in a list, but it lets you work with that item after removing it. The term pop comes from thinking of a list as a stack of items and popping one item off the top of the stack. In this analogy, the top of a stack corresponds to the end of a list.

names = ["Ahmed", "Mohamed", "Ali", "Omar"]
print(names)

popped_name = names.pop()
print(names)
print(popped_name)
['Ahmed', 'Mohamed', 'Ali', 'Omar']
['Ahmed', 'Mohamed', 'Ali']

How might this pop() method be useful? Imagine that the names in the list are stored in chronological order according to when a user register to a website. If this is the case, we can use the pop() method to print a statement about the last registered user.

names = ["Ahmed", "Mohamed", "Ali", "Omar"]
print(names)

last_user = names.pop()
print(names)
print(f"The name of the late registered user on the website is {last_user.lower()}")
['Ahmed', 'Mohamed', 'Ali', 'Omar']
['Ahmed', 'Mohamed', 'Ali']
The name of the late registered user on the website is omar
Popping Items from any Position in a List

You can use pop() to remove an item from any position in a list by including the index of the item you want to remove in parentheses.

names = ["Ahmed", "Mohamed", "Ali", "Omar"]
print(names)

popped_name = names.pop(1)
print(names)
print(popped_name)
['Ahmed', 'Mohamed', 'Ali', 'Omar']
['Ahmed', 'Ali', 'Omar']
Mohamed

Remember that each time you use pop(), the item you work with is no longer stored in the list.

If you’re unsure whether to use the del statement or the pop() method, here’s a simple way to decide: when you want to delete an item from a list and not use that item in any way, use the del statement; if you want to use an item as you remove it, use the pop() method.

Removing an Item by Value

Sometimes you won’t know the position of the value you want to remove from a list. If you only know the value of the item you want to remove, you can use the remove() method.

names = ["Ahmed", "Mohamed", "Ali", "Omar"]
print(names)

names.remove("Ali")
print(names)
['Ahmed', 'Mohamed', 'Ali', 'Omar']
['Ahmed', 'Mohamed', 'Omar']

You can also use the remove() method to work with a value that’s being removed from a list.

names = ["Ahmed", "Mohamed", "Ali", "Omar"]
print(names)

inactive_user = "Ali"

names.remove(inactive_user)
print(names)

print(f"\n{inactive_user.lower()} is an Inactive User.")
['Ahmed', 'Mohamed', 'Ali', 'Omar']
['Ahmed', 'Mohamed', 'Omar']

ali is an Inactive User.

After defining the list, we assign the value "Ali" to a variable called inactive_user. We then use this variable to tell Python which value to remove from the list. Value "Ali" has been removed from the list but is still accessible through the variable inactive_user, allowing us to print a statement about why we removed "Ali" from the list of names.

The remove() method deletes only the first occurrence of the value you specify. If there’s a possibility the value appears more than once in the list, you’ll need to use a loop to make sure all occurrences of the value are removed.

Organizing a List

Often, your lists will be created in an unpredictable order, because you can’t always control the order in which your users provide their data. Although this is unavoidable in most circumstances, you’ll frequently want to present your information in a particular order. Sometimes you’ll want to preserve the original order of your list, and other times you’ll want to change the original order. Python provides a number of different ways to organize your lists, depending on the situation.

Sorting a List Permanently with the sort() Method

Python’s sort() method makes it relatively easy to sort a list. Imagine we have a list of cars and want to change the order of the list to store them alphabetically. To keep the task simple, let’s assume that all the values in the list are lowercase.

cars = ["bmw", "audi", "toyota", "subaru"]
cars.sort()
print(cars)
['audi', 'bmw', 'subaru', 'toyota']

The sort() method changes the order of the list permanently. The cars are now in alphabetical order, and we can never revert to the original order.

You can also sort this list in reverse alphabetical order by passing the argument reverse=True to the sort() method. The following example sorts the list of cars in reverse alphabetical order.

cars = ["bmw", "audi", "toyota", "subaru"]
cars.sort(reverse = True)
print(cars)
['toyota', 'subaru', 'bmw', 'audi']

Again, the order of the list is permanently changed.

Sorting a List Temporarily with the sorted() Function

To maintain the original order of a list but present it in a sorted order, you can use the sorted() function. The sorted() function lets you display your list in a particular order but doesn’t affect the actual order of the list.

cars = ["bmw", "audi", "toyota", "subaru"]

print("Here is the original list:")
print(cars)

print("\nHere is the sorted list:")
print(sorted(cars))

print("\nHere is the original list again:")
print(cars)
Here is the original list:
['bmw', 'audi', 'toyota', 'subaru']

Here is the sorted list:
['audi', 'bmw', 'subaru', 'toyota']

Here is the original list again:
['bmw', 'audi', 'toyota', 'subaru']

Notice that the list still exists in its original order after the sorted() function has been used. The sorted() function can also accept a reverse=True argument if you want to display a list in reverse alphabetical order print(sorted(cars, reverse = True)).

Sorting a list alphabetically is a bit more complicated when all the values are not in lowercase. There are several ways to interpret capital letters when determining a sort order, and specifying the exact order can be more complex.

Printing a List in Reverse Order

To reverse the original order of a list, you can use the reverse() method. If we originally stored the list of cars in chronological order according to when we owned them, we could easily rearrange the list into reverse chronological order.

cars = ["bmw", "audi", "toyota", "subaru"]
print(cars)

cars.reverse()
print(cars)
['bmw', 'audi', 'toyota', 'subaru']
['subaru', 'toyota', 'audi', 'bmw']

Notice that reverse() doesn’t sort backward alphabetically; it simply reverses the order of the list.

The reverse() method changes the order of a list permanently, but you can revert to the original order anytime by applying reverse() to the same list a second time.

Finding the Length of a List

You can quickly find the length of a list by using the len() function.

>>> cars = ["bmw", "audi", "toyota", "subaru"]
>>> len(cars)
4

You’ll find len() useful when you need to identify the number of aliens that still need to be shot down in a game, determine the amount of data you have to manage in a visualization, or figure out the number of registered users on a website, among other tasks.

Python counts the items in a list starting with one, so you shouldn’t run into any off-by-one errors when determining the length of a list.

Avoiding Index Errors When Working with Lists

One type of error is common to see when you’re working with lists for the first time. Let’s say you have a list with three items, and you ask for the fourth item.

motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles[3])
IndexError: list index out of range

Python attempts to give you the item at index 3. But when it searches the list, no item in motorcycles has an index of 3. Because of the off-by-one nature of indexing in lists, this error is typical. People think the third item is item number 3, because they start counting at 1. But in Python the third item is number 2, because it starts indexing at 0.

An index error means Python can’t find an item at the index you requested. If an index error occurs in your program, try adjusting the index you’re asking for by one. Then run the program again to see if the results are correct.

Keep in mind that whenever you want to access the last item in a list you use the index -1. This will always work, even if your list has changed size since the last time you accessed it.

motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles[-1])
suzuki

The index -1 always returns the last item in a list. The only time this approach will cause an error is when you request the last item from an empty list.

motorcycles = []
print(motorcycles[-1])
IndexError: list index out of range

f an index error occurs and you can’t figure out how to resolve it, try printing your list or just printing the length of your list. Your list might look much different than you thought it did, especially if it has been managed dynamically by your program. Seeing the actual list, or the exact number of items in your list, can help you sort out such logical errors.