Modifying Lists
It's often necessary to modify lists after they've been created. You can easily add to or remove items from a list, and you can modify individual items in a list as well.
Adding Elements to a List
We begin with a list of favorite programming languages,
languages = ['Python', 'C++']
print(languages)
which produces the following output:
['Python', 'C++']
There are two ways to add a new element into this list. The first is to use the append()
method, which adds the new element to the end of the list:
languages.append('Java')
print(languages)
The output is:
['Python', 'C++', 'Java']
You should usually prefer append()
to add elements to a list, because it's very efficient.
In comparison, the insert()
method is less efficient, but it gives you more flexibiliy. Using insert()
, you can add a new element at any position in your list by specifying the index of the new item. For example, to add the new language 'C'
at the beginning of the list, you would use the following code:
languages.insert(0, 'C')
print(languages)
which produces the following output:
['C', 'Python', 'C++', 'Java']
If you need to add a new programming language exactly in the middle of the list, you can use the insert()
again:
languages.insert(2, 'Rust')
print(languages)
with the result:
['C', 'Python', 'Rust', 'C++', 'Java']
Removing Elements from a List
Now consider a problem when you want to remove an item from a list.
There are two main ways to go about this task, depending on whether you know the position of the item you want to remove or you know its value.
Let's begin with the case where you know the position of the item you want to remove. You can use pop()
method for this.
Let's see an example:
numbers = [1, 2, 3, 4, 5]
print(numbers) # original list
print(numbers.pop()) # removing last element
print(numbers) # list after pop
which produces the following output:
[1, 2, 3, 4, 5]
5
[1, 2, 3, 4]
Notice the line where we pop()
ed the element:
print(numbers.pop()) # removing last element
In this case the last element is removed. And usually, you should prefer to use pop()
in this way, because it's very efficient.
There's another way to use pop()
. If you want to remove an item from any position in a list, you can use the pop()
method with an index. For example, let's remove the first item from our list:
print(numbers.pop(0)) # removing first element
print(numbers)
which produces the following output:
1
[2, 3, 4]
When used with a positional argument, pop()
is considerably slower but it gives you more flexibility.
Another method for removing items from a list is remove()
.
This method is useful when you know the value of the item you want to remove from the list.
The remove()
method deletes only the first occurrence of the value you specify.
Check this in action:
list = [1, 2, 3, 1, 5, 1]
print(list)
list.remove(1)
print(list)
which gives us:
[1, 2, 3, 1, 5, 1]
[2, 3, 1, 5, 1]
Notice that only the first 1
is removed from the list.
We can apply remove()
few more times:
list.remove(3)
list.remove(1)
print(list)
which leaves us with:
[2, 5, 1]
Note that the last 1
is still in the list.
If we try to remove element which is not in the list that would produce an error.
Clearing a List
If you want to remove everything from a list, you can use the clear()
method. For example,
quarter = ['january', 'february', 'march']
quarter.clear()
print(quarter)
which produces:
[]
Modifying Elements in a List
It's possible to modify elements in a list by assigning new values to them. Let's see an example:
File: modify.py
numbers = [1, 2, 3, 4, 5]
print(numbers)
Here we create a new list of integers and output it to the console:
[1, 2, 3, 4, 5]
We can now update individual elements of the list using assignment operator =
:
numbers[0] = 10
numbers[-1] = numbers[-1] * 10
print(numbers)
Note that we update first and last elements of the list. The index-access rules we've seen before apply here as well. The output of the program is:
[10, 2, 3, 4, 50]
It's even possible to modify slices of a list.
numbers[1:4] = [33]
print(numbers)
which replaces three middle elements of the list with a single element:
[10, 33, 50]