List Slicing in Python
In this tutorial, you are going to learn a very useful feature called list slicing in Python with various examples and use cases.
What is List Slicing in Python?
List slicing is the process of slicing a list in python and extracting a part of it to use for some purpose or to store it in a variable.
There are 2 ways to slice a list in python. The first one is using the [:] syntax and the second one is using the slice() function.
1. List Slicing using [:]
The [:] syntax is used to slice a list in python. This is the most frequently used method to slice a list.
Syntax:
list_name[start:end:step]
Here, start is the index of the first element to be included in the slice. end is the index of the last element to be included in the slice. step is the number of elements to be skipped in between.
Let's see some examples to understand this better.
Example 1: get the first 3 elements of a list
Let's say we have a list my_list and we want to get the first 3 elements of it. Take start as 0 and end as 3. The step is 1 by default.
# list slicing
num = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
sliced = num[0:3]
print(sliced) # [1, 2, 3]
# or
sliced = num[:3]
print(sliced) # [1, 2, 3]
[1, 2, 3] [1, 2, 3]
In the above example, you can see if you omit the start index, it is taken as 0 by default.
Example 2: get list elements from index 2 to 5
To get the elements from 2nd to 5th, start with index 2 and end with index 6.
The last index is not included in the slice. So, the last index is 6 to get the elements from 2nd to 5th.
# list slicing
num = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
sliced = num[2:6]
print(sliced) # [3, 4, 5, 6]
[3, 4, 5, 6]
Example 3: get last 3 elements of a list
To get the last 3 elements of a list, start with index -3 and end with the empty index or the length of the list.
# list slicing
num = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
sliced = num[-3:]
print(sliced) # [8, 9, 10]
# or
sliced = num[-3:len(num)]
print(sliced) # [8, 9, 10]
[8, 9, 10] [8, 9, 10]
Example 4: get first 3 elements and the last 3 elements in reverse order
To get the first 3 elements and the last 3 elements in reverse order:
- Start with getting the first 3 elements.
- Then get the last 3 elements.
- Concatenate the two lists.
- Reverse the list.
Here is the program to do this.
# list slicing
num = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
first = num[:3] # [1, 2, 3]
last = num[-3:] # [8, 9, 10]
sliced = first + last # [1, 2, 3, 8, 9, 10]
sliced.reverse()
print(sliced) # [10, 9, 8, 3, 2, 1]
[10, 9, 8, 3, 2, 1]
2. Slice List using Slice object
Slice object is not something that you can directly use in your program to directly get the elements of a list.
Instead, you first need to create a Slice object and then use it to get the elements of a list (or any other sequence).
Here is the syntax to create a Slice object:
slice(start, end, step)
Let's see some examples to understand this better.
Example 1
Get the first 3 elements of a list.
num = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# create a slice object
s_obj = slice(0, 3)
# using the slice object to get the elements of a list
sliced = num[s_obj]
print(sliced) # [1, 2, 3]
[1, 2, 3]
Example 2
Get the elements last 3 elements of a list.
num = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# create a slice object
s_obj = slice(-3, len(num))
# using the slice object to get the elements of a list
sliced = num[s_obj]
print(sliced) # [8, 9, 10]
[8, 9, 10]
Conclusion
We discussed 2 ways to slice a list in Python. Both have their own advantages.
If you want to get the elements of a list in a single line, then use the first method.
If you want a similar slice pattern throughout your program, then create a slice object once and then use it with all your list or any other sequence throughout your program.
Happy coding!😊