How to slice a list in python
When working with lists, we face situations where we may need a part of the list from one index to the other. Slicing is one of the simpler ways to do this. In this article, we will see different scenarios of slicing with examples.
Slicing
Slicing is the most common and easy way to split lists based on indices.
Syntax:
list_name [initialin: endin + 1: stepin]
Here,
- initialin: The index from which the list has to be sliced. (Optional)
- endin: The index till which the list has to be sliced. (Optional)
- stepin: The gap to step from one index to the other index between the first_index and the end_index. (Optional) (Default: 1)
Points to be noted:
- If the initial index is not mentioned, the list will be sliced from the start.
- If the final index is not mentioned, the list will be sliced till the end.
- Negative indexing is allowed to be used in slicing.
- When sliced, the list is not altered, rather, a copy of the sliced list is made, and we can assign it to a new list and work on it.
- We can work with the list from backward using a negative step index.
- When specifying the end index, we need to give the end index + 1 because the list will be sliced only till the specified index – 1.
- The index we give at the first index is also considered while slicing, unlike the end index.
Example:
list1 = [1, 2, 3, 4] print ("The list from index 1 to 2: ",list1 [1 : 3])
Output:
The list from index 1 to 2: [2, 3]
Explanation:
The end index is given as 3. It is sliced till 3 – 1 = 2nd index. The list is sliced from 1st index to 2nd index: [2, 3]
Here are different cases:
list1 = [1, 2, 3, 4] print ("The list from index 0 to 2: ", list1[0 : 3]) print ("The list from index 0 to 3 with step=2: ", list1[0 : 4 : 2]) print ("The list from the start to 2nd index: ", list1 [ : 3]) print ("The list from the 1st index to the end: ", list1 [ 1 : ]) print ("The list with alternate indices: ", list1 [ : : 2]) print ("The exact same list: ", list1 [ : : ])
Output:
The list from index 0 to 2: [1, 2, 3] The list from index 0 to 3 with step=2: [1, 3] The list from the start to 2nd index: [1, 2, 3] The list from the 1st index to the end: [2, 3, 4] The list with alternate indices: [1, 3] The exact same list: [1, 2, 3, 4]
Understanding:
The original list:
- [0 : 3] : From 0th index to (3 – 1) = 2nd index :
- [0 : 4 : 2]: From 0th index to 3rd index with step value = 2:
- [ : 3]: From the first (0th ) index to 2nd index:
- [1 : ]: From 1st index to the end of the list (3 + 1 = 4th ):
- [ : : 2]: From the first index to the last index with step value = 2:
Negative indices:
Consider a list:
List1 = [1, 2, 3, 4]. The negative indices will look like this:
The negative indexing starts from the end as -1, -2, -3, -4…
Slicing using Negative indices
Though the negative indices start from right to left, we need to give indices that go from left to right for slicing using negative indices.
To get from -2 to -3rd indices, we need to give:
[-3 : -2] is correct
[-2 : -3] wrong
- Like above, the last index has to be incremented to be considered.
Program:
list1 = [1, 2, 3, 4] print ("The list from index -3 to -2: ", list1[-3 : -1])
Output:
The list from index -3 to -2: [2, 3]
Explanation:
The last index we need: -2: -2 + 1: -1
[-3 : -1] : From -3rd index to -2nd index:
More cases:
list1 = [1, 2, 3, 4] print ("The list from index -3 to -2: ", list1 [-3 : -1]) print ("The list from index -3 to the end: ", list1 [-3 : ]) print ("The list from the start to -2 index: ", list1 [ : -1]) print ("The list with alternate elements: ", list1 [-4 : : 2])
Output:
The list from index -3 to -2: [2, 3] The list from index -3 to the end: [2, 3, 4] The list from the start to -2 index: [1, 2, 3] The list with alternate elements: [1, 3]
Understanding:
- [-3 : -1] -> from -3rd index to -1-1 = -2nd index:
- [-3 : ] -> from -3rd index till the end:
- [ : -1] -> from the start to -1 -> -1-1 = -2nd index:
- [-4 : : 2] = step = 2:
Note: If we want the list to the end, we cannot give -1+1 = 0 index because the 0th index means the first element in the positive indices. So, we should leave it without giving any index.
Slicing with Negative step:
We can even give the step-index as negative. This parses the list in the reverse order from right to left.
Program:
list1 = [1, 2, 3, 4] print ("The reverse string: ",list1 [ : : -1]) print ("Negative step 2", list1 [ : : -2]) print ("Negative step 3",list1 [ : : -3]) print ("Negative step 4",list1 [ : : -4])
Output:
The reverse string: [4, 3, 2, 1] Negative step 2 [4, 2] Negative step 3 [4, 1] Negative step 4 [4]
Understanding:
It is the same as the above conditions. The only change is the parsing is done from right to left:
Step = -1:
Step = -2:
This is the concept of slicing.
Here are some more examples:
Slicing when we don’t know the length of the list:
In this case, we can use a function in Python called the len() to find the length of the list.
Program:
list1 = [23, 78, 45, 68, 34, 21] length = len (list1) print ("The length of the list is ", length) halve = length // 2 print ("Slicing the list into halve: ", list1 [ : halve]) print ("Slicing for the second halve: ",list1 [halve : ])
Output:
The length of the list is 6 Slicing the list into half: [23, 78, 45] Slicing for the second half: [68, 34, 21]
Understanding:
Here, we made our list. But, in some scenarios, we won't know the details of the list. In such cases, we can use the len () function to access the length of the list. The last index of the list will be length – 1. Now, we can easily go for slicing.
slice () function in Python:
Example program:
a = [1, 2, 3, 4, 5, 6] x = slice (3, 5) print (a [x])
Output:
[4, 5]
Explanation: The slice function does the same as we saw in above section. To use the function, we need to give the indices we need in the slice function and assign them to a variable. Then, we need to give that as an index to the list to get the sliced list.
Example:
list1 = [23, 78, 45, 68, 34, 21] length = len (list1) print ("The length of the list is ", length) halve = length // 2 a = slice (0 , halve) print ("Slicing the list into halve: ", list1 [a])
Output:
The length of the list is 6 Slicing the list into half: [23, 78, 45]