In this article, we begin to study lists in Python and learn how to operate list elements efficiently.
List introduction
A list is an ordered collection of elements. Python uses square brackets ([]) to define the list. The following is an empty list:
empty_list = []
Typically, a list contains one or more elements. To distinguish multiple elements, we need to use commas. For example:
todo_list = ['Learn Python List','How to manage List elements']
Since lists usually contain multiple elements, plural names are recommended, such as numbers, colors, and shopping_carts. The following example defines a list of six numbers:
numbers = [1, 3, 2, 7, 9, 4]
If we print the list, the output will contain square brackets. For example:
print(numbers) [1, 3, 2, 7, 9, 4]
The following example defines a list of strings:
colors = ['red', 'green', 'blue'] print(colors)
The output results are as follows:
['red', 'green', 'blue']
The elements of the list can be other lists. The following example defines a list composed of lists:
coordinates = [[0, 0], [100, 100], [200, 200]] print(coordinates)
The output results are as follows:
[[0, 0], [100, 100], [200, 200]]
Access list elements
A list is an ordered collection, so we can access its elements through subscripts:
list[index]
The subscript of the first element of the list is 0, the subscript of the second element is 1, and so on.
The following code demonstrates how to access the first element of the list numbers:
numbers = [1, 3, 2, 7, 9, 4] print(numbers[0])
The output results are as follows:
1
numbers[1] will return the second element of the list:
numbers = [1, 3, 2, 7, 9, 4] print(numbers[1])
The output results are as follows:
3
Negative subscripts can return elements from the right side of the list. list[-1] returns the last element, list[-2] returns the penultimate element, and so on. For example:
numbers = [1, 3, 2, 7, 9, 4] print(numbers[-1]) print(numbers[-2])
The output results are as follows:
4 9
Modify, add, and delete elements
A list is a dynamic array, which means that we can modify, add and delete elements in the list.
Modify list elements
To modify a list element, we can specify a new data for it using the following syntax:
list[index] = new_value
The following code demonstrates how to change the first element of the numbers list to 10:
numbers = [1, 3, 2, 7, 9, 4] numbers[0] = 10 print(numbers)
The output results are as follows:
[10, 3, 2, 7, 9, 4]
The following example multiplies the second element of the numbers list by 10:
numbers = [1, 3, 2, 7, 9, 4] numbers[1] = numbers[1]*10 print(numbers)
The output results are as follows:
[1, 30, 2, 7, 9, 4]
The following example divides the third element of the list numbers by 2:
numbers = [1, 3, 2, 7, 9, 4] numbers[2] /= 2 print(numbers)
The output results are as follows:
[1, 3, 1.0, 7, 9, 4]
Add list element
The append() method of the list is used to add an element at the end. For example:
numbers = [1, 3, 2, 7, 9, 4] numbers.append(100) print(numbers)
The output results are as follows:
[1, 3, 2, 7, 9, 4, 100]
The insert() method of the list is used to add an element at the specified position. For example, the following code adds an element 100 after the second element in the list numbers:
numbers = [1, 3, 2, 7, 9, 4] numbers.insert(2, 100) print(numbers)
The output results are as follows:
[1, 3, 100, 2, 7, 9, 4]
Delete list element
The del statement can be used to delete an element at a specified position in the list. The following example deletes the first element in the list numbers:
numbers = [1, 3, 2, 7, 9, 4] del numbers[0] print(numbers)
The output results are as follows:
[3, 2, 7, 9, 4]
The pop() method of the list can delete the last element and return it:
numbers = [1, 3, 2, 7, 9, 4] last = numbers.pop() print(last) print(numbers)
The output results are as follows:
4 [1, 3, 2, 7, 9]
If we want to delete an element in the list and access the value of that element, we can use the pop() method.
The pop() method can delete the specified element by subscript, for example:
numbers = [1, 3, 2, 7, 9, 4] second = numbers.pop(1) print(second) print(numbers)
The output results are as follows:
3 [1, 2, 7, 9, 4]
If you want to delete elements from data, you can use the remove() method. For example, the following code deletes the first element with a value of 9 in the list numbers:
numbers = [1, 3, 2, 7, 9, 4, 9] numbers.remove(9) print(numbers)
The output results are as follows:
[1, 3, 2, 7, 4, 9]
Note that the second element 9 in the list is not deleted.
summary
- A list is an ordered collection of elements.
- Use square brackets ([]) and subscripts to access the elements in the list, and the subscript of the first element is 0.
- A negative subscript indicates that the list element is accessed from the right, and the subscript of the last element is - 1.
- Use list[index] = new_value modifies the element value in the list.
- Use the append() method to append a list element.
- Insert the element at the specified position using the insert() method.
- Use the pop() method to delete an element and return its value.
- Use the remove() method to delete the element.