Lists are a fundamental data structure widely used in every major programming language. Python is not the exception. Lists in Python are heavily used and for that reason they're quite powerful.
Here are the fundamental things you need to know about lists in Python.
Check out our course: Introduction to Programming with Python
To create a list, just surround your elements between square brackets (
my_list = [1, 2, 3, 4] # A list containing integers my_list = ['a', 'b', 'c', 'd'] # A list containing strings
Lists in Python are:
# Ordered my_list = ['z', 'b', 'r', 'd'] print(my_list) # ['z', 'b', 'r', 'd'] (Order is kept) # Mutable my_list = ['a', 'b', 'c'] my_list.append('d') # Add an element to the end my_list.insert(0, 'z') # Add an element to the beginning # Heterogeneous my_list = [9, 'rmotr', date(2016, 1, 1)] # Different object types in the same list
Once you have a list, you'll want to perform different operations with it. For example, counting elements, adding new elements, removing others, etc.
Python makes it easy to operate with your list. There are many different operations you can perform, we just show you a few examples for you to see how useful they are, but we encourage you to check the documentation for more.
my_list = ['a', 'b', 'c'] # Append an element (add at the end) my_list.append('d') # Add an element in a certain position my_list.insert(0, 'z') # Beginning my_list.insert(3, 'z') # 4th position (index == 3) # Count how many items the list has print(len(my_list)) # Remove all the elements from the list my_list.clear() # Reverse its items my_list.reverse() # ... many more ...
Iteration is key to understand how lists work. To properly understand lists you should be familiar with the for loop (control flow statement).
The good news is that it's simple to understand. Here are a few examples:
names = ['Mary', 'Tom', 'Rose'] for name in names: print(name) # This will print the names, each in a new line: # Mary # Tom # Rose numbers = [0, 1, 2, 3, 4] for number in numbers: # I can define whatever I want inside the **for loop body** double = number * 2 print(double) # This will print the numbers, doubled (one number per line): # 0 # 2 # 4 # 6 # 8
As you can see in the previous example, a
for loop is composed of a few things:
for is a keyword, should always be used
name in this case is a variable that we choose. We can choose whatever name we want for this variable (
a_name). It'll reference each one of the elements in the list
in is also a keyword. It precedes the list we'll iterate.
names is the list itself. It's the list we want to iterate
Note 5: Finally, the for loop body. This is really important. We'll express here whatever we want to do with each one of the elements as we iterate through the list.
Finally, you can retrieve individual elements from a list by specifying the position they have in the list. Indexes start from the number 0. So, the first element will have index 0, the second one will have index 1, the third one 3, etc... The last element of the list has index
len(list) - 1 (if the list has 8 elements, the last one is 7 (
There are also negative indexes, the last element of the list also has the index -1, the second to last -2, etc.
names = ['Mary', 'Tom', 'Rose', 'Joe'] mary = names tom = names joe = names[-1] rose = names[-2]
You can also get sublists from a given list. That'd be like a fraction of a list. A sublist specified by a range of indexes. For example, the elements between the second and the fifth position, the first three elements, etc.
To get a sublist you must specify two indexes separated by a colon sign. Example:
Important! The resulting sublist will NOT include the element at the last position specified. See examples below
names = ['Mary', 'Tom', 'Rose', 'Joe'] # First 3 names print(names[0:3]) # ['Mary', 'Tom', 'Rose'] # Just the first name print(names[0:1]) # ['Mary'] # Second and third print(names[1:3]) # [Tom', 'Rose'] # From the second element up to the end of the list print(names[1:4]) # ['Tom', 'Rose', 'Joe'] # The first index will be 0 by default, we can omit it: print(names[:3]) # ['Mary', 'Tom', 'Rose'] # The last index will be the last element by default, we can also omit it print(names[1:]) # ['Tom', 'Rose', 'Joe']