You already know how to create lists (even empty ones), so, no wonder, that you may want to change your lists somehow. There are lots of things to do with a list, you can read about them in the Python Data Structures documentation. In this topic, we will discuss only the basic operations.
Adding one element
The list is a dynamic collection, which means you can add and remove elements. To take a closer look, let's create an empty list of dragons.
dragons = [] # we do not have dragons yet
What is next? The first thing that comes to mind is, of course, to add new elements to the list.
To add a new element to the end of an existing list, you need to use the list.append(element) method. It takes only a single argument, so this way you can add only one element to the list at a time.
dragons.append('Rudror')
dragons.append('Targiss')
dragons.append('Coporth')
Now you have three dragons, and they are ordered the way you added them:
print(dragons) # ['Rudror', 'Targiss', 'Coporth']Adding several elements
There is the list.extend(another_list) operation that adds all the elements from another iterable to the end of a list.
numbers = [1, 2, 3, 4, 5]
numbers.extend([10, 20, 30])
print(numbers) # [1, 2, 3, 4, 5, 10, 20, 30]
Be careful — if you use list.append(another_list) instead of list.extend(another_list), it adds the entire list as an element:
numbers = [1, 2, 3, 4, 5]
numbers.append([10, 20, 30])
print(numbers) # [1, 2, 3, 4, 5, [10, 20, 30]]
Alternatively, to merge two lists, you can just add one to another:
numbers_to_four = [0, 1, 2, 3, 4]
numbers_from_five = [5, 6, 7, 8, 9]
numbers_from_ten = [10, 11, 12, 13, 14]
numbers = numbers_to_four + numbers_from_five
print(numbers) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
numbers += numbers_from_ten
print(numbers) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
If you need a list with repeating elements, you can create a list with the repeating pattern, and then just multiply it by any number. This is particularly useful when you want to create a list of a specific length with the same value:
pattern = ['a', 'b', 'c']
patterns = pattern * 3
print(patterns) # ['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c']
one = [1]
ones = one * 7
print(ones) # [1, 1, 1, 1, 1, 1, 1]Removing elements
The opposite of adding elements — deleting them — can be done in three ways. Let's have a look at them.
First, we can use the list.remove(element) operation.
dragons.remove('Targiss')
print(dragons) # ['Rudror', 'Coporth']
If the element we want to delete occurs several times in the list, only the first instance of that element is removed.
dragons = ['Rudror', 'Targiss', 'Coporth', 'Targiss']
dragons.remove('Targiss')
print(dragons) # ['Rudror', 'Coporth', 'Targiss']
The other two ways remove elements by their indexes rather than the values themselves. The del keyword deletes any kind of objects in Python, so it can be used to remove specific elements in a list:
dragons = ['Rudror', 'Targiss', 'Coporth']
del dragons[1]
print(dragons) # ['Rudror', 'Coporth']
Finally, there is the list.pop() method. If used without arguments, it removes and returns the last element in the list.
dragons = ['Rudror', 'Targiss', 'Coporth']
last_dragon = dragons.pop()
print(last_dragon) # 'Coporth'
print(dragons) # ['Rudror', 'Targiss']
Alternatively, we can specify the index of the element we want to remove and return:
dragons = ['Rudror', 'Targiss', 'Coporth']
first_dragon = dragons.pop(0)
print(first_dragon) # 'Rudror'
print(dragons) # ['Targiss', 'Coporth']
Our discussion of the basic operations with lists has come to an end. If you need more information, check out the Python Data Structures documentation.
Summary
Summing up, in this topic we've learned to:
- add an element with
append(); - add several elements with
extend(); - remove elements with
remove();
We also know now that since the list is a dynamic collection, it can be changed. There is no need to constantly create new lists, which benefits the memory and performance of your program.