How to Use Lists in Python (13 Examples)
In this tutorial, you’ll learn how to utilize lists in Python. The documentation about Python lists can be found here.
The content of the post looks as follows:
Let’s start right away.
What are Lists?
In Python, a list is a type of data structure that allows you to store a collection of items, such as numbers, strings, or even other lists.
Lists are mutable, meaning you can change their contents by adding, removing, or modifying elements. They are also ordered, so you can access items in a specific order using their index.
Lists are commonly used in Python programming to represent sequences of data, and they are a versatile and powerful tool for working with data in Python.
Below, I’ll show you several examples of how to use lists in Python. Keep on reading!
Example 1: Initializing Single-Type List
The following Python syntax demonstrates how to create a list of elements in the same data type.
single_dtype_list = [1, 5, 7] print(single_dtype_list) # [1, 5, 7]
Just like that, we have created a single-type list.
Example 2: Initializing Multi-Type List
The list can be composed of elements of various types as well.
multi_dtype_list = [1, "abc", 7, 'n'] print(multi_dtype_list) # [1, 'abc', 7, 'n']
As you can see, there are no syntax differences, Python lists support elements of different data types by default.
Example 3: Initializing Empty List
An empty list can be generated by leaving the middle of brackets empty.
empty_list =  print(empty_list) # 
The output shows the list has no elements in it.
Example 4: Accessing Single Element of List
In the previous examples, I have demonstrated how to create different types of list objects. However, for the sake of simplicity, we’ll use the same example list in the remaining examples.
Our new example list is created below:
my_list = [0, 3, -2, 15, 50, 28, 1]
Elements in the list can be accessed by using the indices of the elements.
print(my_list) # printing all of my_list # [0, 3, -2, 15, 50, 28, 1] print(my_list) # printing a single element of my_list # 15
By executing the code line above, we can access the 4th element of the list with the index number 3. Please be aware that indexing starts from 0 in Python.
Python also allows accessing elements in the reverse order, i.e., with negative numbers.
print(my_list[-2]) # printing second from last element # 28
As shown above, the second element from the end of the list is printed using the index -2.
Example 5: Accessing Multiple Elements of List
We use two parameters indicating the starting and ending indexes to get multiple elements.
print(my_list[2:5]) # printing multiple elements of the list # [-2, 15, 50]
Here the elements with index numbers 2,3,4 were printed since the first parameter is inclusive, while the second parameter is exclusive.
You can also leave one of the parameters blank, which returns the elements until the end of the lists starting from the given parameter, or returns the elements from the beginning of the list until the given parameter. See below.
print(my_list[2:]) # printing elements with index >= 2 # [-2, 15, 50, 28, 1] print(my_list[:4]) # printing elements with index < 4 # [0, 3, -2, 15]
Furthermore, we can get every second/third/… element using a third parameter (step parameter) that indicates the increment between each index.
print(my_list[1:5:2]) # printing elements with 1 <= index < 5, step = 2 # [3, 15]
In case you would like to learn more on how to access multiple list elements, please have a look here.
In the final output, indexes 2 and 4 are skipped since the step size is 2. The rest of the list is not considered because the ending index is 5.
Example 6: Modifying Elements of List
Modifying the elements is a two-step process of accessing the element and updating the value.
print(my_list) # printing initial state of my_list # [0, 3, -2, 15, 50, 28, 1] my_list = 8 print(my_list) # printing altered state of my_list # [0, 3, -2, 8, 50, 28, 1]
The new value is assigned via
= sign after accessing the element by its index. In this example, the element at index 3 was changed to 8.
If you are keen to know more about modifying a list or even creating a list by transforming and converting, you can have a look at the following tutorial where a dictionary is converted to a list in Python.
Example 7: Adding New Elements to List via append() Method
The append() method can be used for adding an element to the end of the list.
print(my_list) # printing initial state of my_list # [0, 3, -2, 8, 50, 28, 1] my_list.append(18) # add 18 to the end of my_list print(my_list) # printing altered state of my_list # [0, 3, -2, 8, 50, 28, 1, 18]
In this case, number 18 was added to the end of the list.
Here is another turorial where you can find more information on how to use the append() method in Python.
Example 8: Adding New Elements to List via insert() Method
If we want to add an element to a specific position, then we can use the insert() method.
my_list.insert(3, 100) # inserting 100 at index 3 print(my_list) # printing altered state of my_list # [0, 3, -2, 100, 8, 50, 28, 1, 18]
As you can see, number 100 was added at index 3.
If you want to learn more about the insert() method in Python you can have a look here.
Example 9: Removing Elements from List via remove() Method
Just like adding an element, there are two functions for removing elements. The first option is using the remove() method, which removes the element specified.
my_list.remove(100) # removing the element with the value 100 print(my_list) # printing altered my_list # [0, 3, -2, 8, 50, 28, 1, 18]
Here, number 100 was removed from the list.
Of course, there are many more options and opportunities. For example, you can also remove all occurrences of a character in a list.
Example 10: Removing Elements from List via pop() Method
The second option is using the pop() method, which removes the element at the specified index.
my_list.pop(-1) # removing the last element print(my_list) # printing altered my_list # [0, 3, -2, 8, 50, 28, 1]
As you can see, the last element, 18, was removed from my_list.
If you are interested in other methods of clearing elements in a list, have a look at this tutorial.
Example 11: Finding Index of Element
We can find out the index of an element via the index() method.
print(my_list.index(50)) # printing the index of number 50 # 4
This shouldn’t be confused with accessing an element by index. Here the access flow is reversed. Please compare the example with Examples 4 and 5.
In addition, here you can find another tutorial on how to get the index of a list element conditionally.
Example 12: Sorting List
To sort a list, we can utilize the sort() method.
print(my_list) # printing original my_list # [0, 3, -2, 8, 50, 28, 1] my_list.sort() # sorting my_list print(my_list) # printing sorted my_list # [-2, 0, 1, 3, 8, 28, 50]
In the second output, we see the elements of my_list have been sorted in ascending order.
Of course, when it comes to sorting a list in Python, there are many different use cases like sorting a list of integers, sorting a list of strings, sorting a list of floats, or even sorting a list of datetime objects.
Example 13: Copying List
Lastly, if we need to make another copy of the list, we can utilize the copy() method.
deep_copy = my_list.copy() # copying my_list to deep_copy print(deep_copy) # printing the deep_copy # [-2, 0, 1, 3, 8, 28, 50]
By executing the command above, we have a fully separate (deep) copy of my_list. Hence, any modifications on either of the copies won’t be reflected on the other copy. A common mistake could be trying the following method.
shallow_copy = my_list shallow_copy = -123
Let’s see how the final state of our lists turned out to be.
print(shallow_copy) print(my_list) print(deep_copy) # [-123, 0, 1, 3, 8, 28, 50] # [-123, 0, 1, 3, 8, 28, 50] # [-2, 0, 1, 3, 8, 28, 50]
Although any changes made on shallow_copy are seen on my_list as well, deep_copy is not affected.
Video, Further Resources & Summary
I have recently released a video on my YouTube channel, which demonstrates the content of this article. Please find the video below.
The YouTube video will be added soon.
Also, you might want to have a look at the other articles on my website. I have published several tutorials already.
- Access Dictionary within List in Python
- Access Index of List in Python
- Access List Index in for Loop in Python
- List Comprehension in Python
- count() Method for Lists in Python
- Python Programming Overview
At this point, you should have learned how lists function in Python programming. In case you have further comments and/or questions, don’t hesitate to let me know in the comments.
This page was created in collaboration with Ömer Ekiz. Look at Ömer’s author page to get further information about his professional background, a list of all his tutorials, as well as an overview of his other tasks on Statistics Globe.
Statistics Globe Newsletter