Aug 13th, 2021 - written by Kimserey with .
Lists are one of the most versatile structures in Python. They allow us to keep lists elements which can be of different types and access them by index with constant time complexity. One of the best features of lists in Python is the slicing mechanism.
We start first with a list:
1
>>> a = [1, 2, 3, 4, 5, 6]
We can then access specific index:
1
2
>>> a[1]
>>> 2
Slicing can be done with :
where the syntax is a[{first}:{last}:{step}]
:
first
would be the index of the first element,last
would be the last element excluded,step
is an optional step to iterate over the array, by default step is 1.If we want to slice the array from the second element till the end we can use 1:
:
1
2
>>> a[1:]
>>> [2, 3, 4, 5, 6]
If we want to slice from the end, we can use a[:3]
, we would get the element from index 0 till 3 excluded:
1
2
>>> a[:3]
>>> [1, 2, 3]
If we don’t specify any indexes, we would simply get the whole list back:
1
2
>>> a[:]
>>> [1, 2, 3, 4, 5, 6]
This can be used to execute a shallow copy of the list (similar to list.copy()
).
Lastly we can use the step
to skip elements:
1
2
>>> a[0:10:2]
>>> [1, 3, 5]
Which would be the same as ::2
as 0
is the first index.
1
2
>>> a[::2]
>>> [1, 3, 5]
Another interesting part is that the slicing does not have to be indexes part of the list. If we specify a slicing out of range, it will return empty for those element for example:
1
2
>>> a[100:130]
>>> []
We can also set the step as -1
with [::-1]
and it will reverse the array:
1
>>> a[::-1]
We can also use slicing to assign part of the list. For example, if we want to append a list to the beginning we can achieve it in this way:
1
2
3
4
5
6
7
8
9
10
11
12
>>> a = [1,2,3]
>>> a
>>> [1, 2, 3]
>>> a[:0]
>>> []
>>> a[:0] = [-2, -1, 0]
>>> a
>>> [-2, -1, 0, 1, 2, 3]
Here we leverage the fact that a[:0]
reference the beginning of the list up to index 0
excluded which would be empty list. By assigning it we can prepend values to the list.
Following the same logic, we can either reassign part of the list, or append at the end:
1
2
3
4
5
6
7
8
9
10
>>> a
>>> [1, 2, 3]
>>> a[1:3]
>>> 2, 3]
>>> a[1:3] = [4, 5]
>>> a
>>> [1, 4, 5]
We see here that we have reassigned index 1
and 2
by reassigning slicing a[1:3]
.
1
2
3
4
5
6
7
>>> a[len(a):]
>>> []
>>> a[len(a):] = [10, 11]
>>> a
>>> [1, 4, 5, 10, 11]
And using the same method, we reassigned a[len(a):]
which would be the end of the list. Again note that len(a)
is not a valid index for a
as len(a)
would be one item after the end of the list but the slicing will return end of list as we seen earlier.
Lastly an interesting point is to remove part of a list.
For creating a new list we can simply use slicing and concatenate the parts, for example if we want to remove between index 1
and 3
, we could do:
1
2
3
4
5
6
>>> a = [1, 2, 3, 4, 5, 6]
>>> a = a[:1] + a[4:]
>>> a
>>> [1, 5, 6]
Or we can do a reassignment:
1
2
3
4
5
6
7
8
>>> a
>>> [1, 5, 6]
>>> a[1:2]
>>> 5
>>> a[1:2] = []
>>> [1, 6]
Or we can remove elements in place using del
:
1
2
3
4
>>> del a[1:4]
>>> a
>>> [1, 5, 6]
Today we looked at the different slicing options for list in Python. We saw how versatile they are and how they can be used to take parts of the list.Lastly we completed the post by looking at how we could remove a portion of the list. Hope you like this post and I see you on the next one!