List Slicing In Python
Now, we are going to discuss the concept of list slicing. We perform list slicing in order to access a range of elements from the list. List slicing is very simple. Here, we just need to make use of the slicing operator, which is just a colon. First of all, Let’s have a look at the syntax of how are we going to slice the list.
As you can see, this is somewhat similar to how we access the elements. But here, since we are going to access a range of elements, we are going to provide some more things here, like the start, stop and the step. Let’s have a look at a simple example, through which, we can get a broader idea about the list slicing, and also about giving the start, stop and step here.
List Slicing In Python
As you can see in the above program, we have a simple list, and in the next line, we are trying to access a range of elements from the list. Now, inside the square brackets, you can see that we have specified the start, and then after the colon, we have specified the stop. Basically, doing this will let us access the elements from index 1 to index 4. So, the stop it’self is excluded. So, Let’s have a look at the output of the above program.
[2, 3, 4, True]
As you can see, we could access the elements from the index 1, to the index 4. Also observe that this is also a list. So, this is like we are slicing another list, from the list that we have, and the new list contains a range of elements from another list.
Also, you might have noticed that we have not given the stop value. Since we haven’t given any, it is considered as 1. So, you can consider that the start tells the index, from which we want to perform slicing. The stop value specifies where to end (the last element in the sliced list is from the index stop – 1), and the step can be considered as an increment after accessing one from the list. Let’s have a look at another program, which gives us an idea about using the step.
As you can see in the above program, we have specified the start as 1, the stop as 6, and the step as 2. So, here we are trying to access some elements in a particular range. The thing is that we have declared the step as 2, so this is like when we are accessing some element, move two steps forward to access the next element. So, this is like if we have accessed the element from index 0, the next element that we are going to accessed would be at index 2. Let’s now have a look at the output.
[2, 4, ‘GyaniPandit’]
As you can see, the first element that we have got is 2, which is the index 0 element from the original list, and then the next element is 4, which is the index 2 element from the original list. This was because we had used the step as 2.
You can make use of the list slicing, to get a range of elements from our list. This can be a requirement at times, in our python programs.
Now, Let’s try to omit the values for the start, stop, and the step as well. The thing is that if we are doing this, we simply get the complete list. So, if we want to have a copy of a list, we can simply do this with the help of list slicing. Let’s have a look at a simple program, which demonstrates us about the same thing.
As you can see, we have a list, and then we are trying to perform list slicing. Here, we have omitted the start, stop, and the step values. So, if we are doing this, we are getting a copy of that list, which is being assigned to the another_list variable. So, in the above program, we get the copy of the list, assigned to another variable, which is not possible if do something like this –
As you can see in the above program, we are directly doing another_list = my_list. Doing this is not going to create a copy of the list, but the another_list reference variable will start pointing to the same list, as the my_list reference variable. The output might seem to be same, but in the first case, both the variables are pointing to different lists, and in the later case, both the reference variables are pointing to same lists.
We can also perform list slicing with the negative indexing. Let’s have a look at a simple program, which tries to demonstrate how to perform list slicing with negative indexing.
As you can see in the above program, you can simply use the negative indexing here as well. The thing is that we get the elements from the index -5 to -2, since the stop value it’self is excluded. The output of the above program is something like this –
[2, 3, 4, True]
As you can see, we got the elements from the index -5, and the index of the last element is -2. So, this way, we can also make use of the negative indexing here.