Intro to Programming: Lists
Basics of Python Syntax / Intro to Programming
This lesson is written in the co-authorship with artificial intelligence and lots of manual edits of a human being. The rest of the course can be found here: “Intro to Programming”.
In a previous lesson, we discussed primitive data types used in Python. Besides those data types, there are also complex ones — so called, data structures. List is one of such examples.
Lists are a very useful tool in programming because they allow us to store and manipulate large amounts of data in a single place. Without lists, we would need to store data in separate variables, which would be very difficult to manage if we had a lot of data.
For example, let’s say we wanted to store the names of 10 students in a class. Without lists, we might do something like this:
student1 = 'Alice'
student2 = 'Bob'
student3 = 'Charlie'
student4 = 'David'
student5 = 'Eve'
student6 = 'Frank'
student7 = 'Greta'
student8 = 'Henry'
student9 = 'Igor'
student10 = 'Jenny'
This would be very difficult to manage, because we would have to create 10 separate variables and remember their names. If we wanted to access a particular student’s name, we would have to remember which variable we used to store it.
With lists, we can store all of this information in a single place, like this:
students = ['Alice', 'Bob', 'Charlie', 'David', 'Eve', 'Frank', 'Greta', 'Henry', 'Igor', 'Jenny']
In this example, students
is the name of our list, and it contains ten names: from 'Alice' to 'Jenny'. Each item in the list is called an "element".
Comparing to multiple variables (one per each student), using a list is much easier to manage, because we only have to remember the name of the list (students
) and we can access any student's name by using their index.
Index — an address of a list element
The index is the position of an element in the list, and it starts at 0 for the first element. For example, we can access the first element of the students
like this:
first_item = students[0]
print(first_item) # Output: 'Alice'
Note, that it starts at 0 for the first element.
Mutating a List
There are several ways to “mutate” a list in Python, which means to change its contents. Here are some common ways to mutate a list:
Modifying an element: You can use the index of an element to change its value. For example:
shopping_list = ['apples', 'oranges', 'bananas']
shopping_list[1] = 'grapes' # Modify the second element of the list
print(shopping_list) # Output: ['apples', 'grapes', 'bananas']
Adding an element: You can use the append()
method to add a new element to the end of a list. For example:
shopping_list = ['apples', 'oranges', 'bananas']
shopping_list.append('mangoes') # Add 'mangoes' to the end of the list
print(shopping_list) # Output: ['apples', 'oranges', 'bananas', 'mangoes']
You can also use the insert()
method to add a new element at a specific position in the list. For example:
shopping_list = ['apples', 'oranges', 'bananas']
shopping_list.insert(1, 'grapes') # Add 'grapes' at index 1 (second position)
print(shopping_list) # Output: ['apples', 'grapes', 'oranges', 'bananas']
Removing an element: You can use the remove()
method to remove an element from a list. For example:
shopping_list = ['apples', 'oranges', 'bananas']
shopping_list.remove('oranges') # Remove the element 'oranges' from the list
print(shopping_list) # Output: ['apples', 'bananas']
You can also use the pop()
method to remove an element from a specific position in the list. For example:
shopping_list = ['apples', 'oranges', 'bananas']
last_item = shopping_list.pop() # Remove the last element of the list
print(last_item) # Output: 'bananas'
print(shopping_list) # Output: ['apples', 'oranges']
Nice, I hope you understand what Lists are better now. Loops is a great topic to learn to complement the ways of working with lists. See this lesson for that: https://medium.com/@yeldos/intro-to-programming-loops-565995032bdd