In the previous article, we learned how to iterate over several values using a for loop.
For example:
for number in range(1, 6):
print(number)
But what if we have a set of data that we want to store and use later?
For example, a shopping list:
bread
milk
apples
coffee
For this, Python provides lists.
Creating a list
A list is written using square brackets:
shopping_list = ["bread", "milk", "apples", "coffee"]
Now all four values are stored in a single list.
We can print the entire list:
print(shopping_list)
We get:
['bread', 'milk', 'apples', 'coffee']
A list can contain not only strings.
For example:
numbers = [10, 20, 30, 40]
It can even contain values of different types:
items = ["Anna", 25, True]
In practice, however, the elements of a list usually have something in common.
Getting an element by index
Each element in a list has its own number — an index.
For example:
shopping_list = ["bread", "milk", "apples", "coffee"]
The indexes look like this:
0 1 2 3
"bread" "milk" "apples" "coffee"
Notice that indexing starts at 0.
To get an element, specify its index inside square brackets:
print(shopping_list[0])
The result:
bread
And:
print(shopping_list[2])
will output:
apples
Changing an element
A list is mutable.
For example:
shopping_list = ["bread", "milk", "apples"]
shopping_list[1] = "cheese"
print(shopping_list)
We get:
['bread', 'cheese', 'apples']
We replaced the second element of the list.
Adding elements
To add a new element, use the append() method.
shopping_list = ["bread", "milk"]
shopping_list.append("coffee")
print(shopping_list)
The result:
['bread', 'milk', 'coffee']
The new element was added to the end of the list.
We can add elements multiple times:
shopping_list.append("apples")
shopping_list.append("cheese")
Now the list contains:
['bread', 'milk', 'coffee', 'apples', 'cheese']
Removing elements
To remove an element by its value, we can use remove().
shopping_list = ["bread", "milk", "apples"]
shopping_list.remove("milk")
print(shopping_list)
We get:
['bread', 'apples']
The remove() method searches for the specified value and removes it from the list.
Iterating over a list
Now let’s combine what we already know.
We have a list:
shopping_list = ["bread", "milk", "apples"]
And we already know how to use for.
So we can write:
for item in shopping_list:
print(item)
The result:
bread
milk
apples
This is one of the most common operations with lists.
We don’t need to know the indexes of the elements.
We can simply ask Python to give us each element one by one.
A list can be empty
Sometimes we need to create a list that doesn’t contain anything yet.
We can simply write:
shopping_list = []
Later, we can add elements:
shopping_list.append("bread")
shopping_list.append("milk")
Now the list contains two elements.
This is especially useful when we are collecting data gradually.
What we learned today
Today we learned about lists — Python’s first collection type.
- A list is created using square brackets
[]. - Each element has an index starting at
0. - An element can be accessed by its index.
- An element can be replaced.
append()adds an element to the end of a list.remove()removes an element by value.- A list can be iterated over using
for.
Try it yourself
Create a list of your favorite movies:
movies = ["Interstellar", "The Matrix", "Terminator"]
Print:
- the entire list;
- the first movie;
- each movie separately.
Then:
- add another movie;
- replace one of the existing movies;
- remove one movie.
Also try creating an empty list and filling it using append().
What’s next?
A list lets us store several values and modify them.
But sometimes we need a collection that cannot be accidentally changed.
For example, the coordinates of a point or a set of values that should remain unchanged.
In the next article, we’ll learn about tuples (tuple).