Python List Slice Tutorial

In this section, we will learn how to slice a List in Python.

What is Python List Slice?

The Python List Slice is a way of taking a range of elements from a list and create a new list as a result. Also, using a slice, we can remove or replace part of elements in a list with new elements.

Python List Slice Syntax:

listName[start: stop]

listName[start: stop: step]

`listName`: this is the name of the list we want to run slice operation on it.

`start`: this is an index number where we want to specify where the slicing should start. This value is optional and, if ignored, the default value, which is 0 (the first element of the list) will be used.

`stop`: this is the index number where we want to stop the slicing. Note that this value is excluded! Meaning if, for example, we set the value 4, the sub-list would contain the elements up to the index number 3! Also, this value is optional and if ignored, the size of the target list will be used.

`step`: this value defines the steps between the `start` and `stop` index. For example, if the value is 2, that means start from the `start` index and then skip one element at a time and add the next one as part of the sub-list. This value is optional and, if ignored, the value +1 will be used.

Example: using python list slice

list1 = [1,2,3,4,5,6,7,8,9,10]

subList = list1[2:8:2]

subList2 = list1[3:7]

print(subList)

print(subList2)

Output:

[3, 5, 7]

[4, 5, 6, 7]

Negative Indexing in Python

Other than using positive values in a slice, we can use negative numbers as well! In this case, negative numbers mean start from the last elements and move towards the beginning of the list.

Note: the last element of a list has the index number -1, the element before the last element of a list has the index number -2 and so on.

Also, the `step` can be set to a negative value as well! One good example of using a negative value for the `step` is for when we want to get the elements of a list in a reverse mode! Basically, if we set the value of `step` to -1 for example, that means we want to reverse the order and move back from the `start` index towards `stop` index. Note that in a situation like this, the value of the `start` index should be greater than the value of the `stop` index (if the values are positive numbers, of course). This is because we’re moving backward from the end of a list (which obviously has a higher index number) towards the beginning (which has a lower index number)!

Example: negative index number for step in slice

list1 = [1,2,3,4,5,6,7,8,9,10]

subList = list1[5:0:-1]

print(subList)

Output:

[6, 5, 4, 3, 2]

In this example, the value of the `step` is set to -1. That means moving backward. Here the value of the `start` index is set to 5 which means the starting position is the index number 5 and from there we want to move back towards the beginning of the list. Also, the value of the `stop` index is set to 0 which means the index number where the sub-list should stop is at this index.

Example: python list slice and negative index numbers

list1 = [1,2,3,4,5,6,7,8,9,10]

subList = list1[-4:-1]

print(subList)

Output:

[7, 8, 9]

Note that the value of the `start` index is less than the value of the `stop` index. This is because the value of the `step` is set to its default, which is +1 and that means the sub-list is taken from the left side of the list towards the right side (end side).

Python List Slice: leaving out the first index

As mentioned at the beginning of this section, the first index of the slice is optional and if ignored, the default value which is 0 will be used instead.

Example: list slice without the first index

list1 = [1,2,3,4,5,6,7,8,9,10]

subList = list1[:6]

print(subList)

Output:

[1, 2, 3, 4, 5, 6]

Python List Slice: leaving out the last index

As mentioned at the beginning of this section, the `stop` index of the slice is optional and if ignored, the default value which is the size of the target list will be used instead.

Example: list slice without the last index

list1 = [1,2,3,4,5,6,7,8,9,10]

subList = list1[3:]

print(subList)

Output:

[4, 5, 6, 7, 8, 9, 10]

Python List Slice: removing elements from a list

So far we’ve seen how to get a sub-list from a list object. But using slice we can remove one or more elements from a list object as well.

To do this, we need to invoke the slicing on the left side of the assignment operator and then put an empty list on the right side of the assignment operator.

Example: removing elements from a list using slice

list1 = [1,2,3,4,5,6,7,8,9,10]

list1[0:1] = []

print(list1)

Output:

[2, 3, 4, 5, 6, 7, 8, 9, 10]

Here, the slice just contains one element and that is the element in the index 0 of the list. As you can see on the right side of the assignment operator there’s just one empty list and as a result, the first element of this list is now removed.

Now let’s remove multiple elements of a list.

Example: removing multiple elements from a list using slicing

list1 = [1,2,3,4,5,6,7,8,9,10]

list1[1:6] = []

print(list1)

Output:

[1, 7, 8, 9, 10]

Python List Slice: adding elements to a list

Using a slice, we can add multiple elements to a list as well!

To do this, we need to put the index number for the `start` and `stop` exactly the same. For example, if we want to add 3 elements to a list at the index number 4, then the value of the `start` and `stop` index should be set to 4.

Example: adding elements to a list using slice

list1 = [1,2,3,4,5,6,7,8,9,10]

list1[1:1] = ["Jack","John","Omid"]

print(list1)

Output:

[1, 'Jack', 'John', 'Omid', 2, 3, 4, 5, 6, 7, 8, 9, 10]

Note that the three new elements are added to the list starting at the index number 1. Also, no old element from the list is removed this way.

Python List Slice: replacing and adding new elements to a list

Using a slice, we can replace old elements of a list with new elements as well!

To do this, we first declare a slice on the left side of the assignment operator and then put new elements as a list on the right side of the assignment operator.

Now here’s the situation:

If the declared slice on the left side of the assignment operator has more elements than the elements of the list on the right side of the assignment operator, then the rest of elements declared in the slice will be removed!

On the other hand, if the number of elements in the slice is less than the number of elements declared in the list on the right side of the assignment operator, then the remaining elements of the list will be “added” to the target list without replacing any old element.

Example: replacing and adding new elements to a list

list1 = [1,2,3,4,5,6,7,8,9,10]

list2 = [10,11,12,13,14,15,16,17,18,19,20]

list1[0:6] = ["Jack","John","Omid"]

print(list1)

list2[0:3] = ["John","Jack","Omid","Ellen","Elon","Ian","Jame","Richard"]

print(list2)

Output:

['Jack', 'John', 'Omid', 7, 8, 9, 10]

['John', 'Jack', 'Omid', 'Ellen', 'Elon', 'Ian', 'Jame', 'Richard', 13, 14, 15, 16, 17, 18, 19, 20]
Facebook
Twitter
Pinterest
LinkedIn

Top Technologies