Today we’ll be talking about slice assignment in Python. It works with lists, which are mutable. We’ll also learn how to use slice assignment to mimic some of the most used list methods. This is not something that is crucial for mastering Python, but take it rather as an exercise. Normally you’d use the methods. That’s what they are for.
Lists are sequences, like tuples or strings, which means their elements are ordered and indexed. All sequences can be sliced. If we slice a string, we obtain a substring. If we slice a list, we obtain a sublist.
Here’s a video version. You can watch it first if you want.
And now, back to our main topic. First of all, what is slice assignment? Well, slice assignment in Python doesn’t work with strings or tuples, because they are immutable, but it works with lists. A list can be changed in place by assigning one or more elements to a slice. In other words, the whole slice is replaced by new elements.
Table of Contents
Examples of Slice Assignment in Python
The number of the elements may be equal to or different than the number of the elements in the slice.
Here are some examples. In the most basic scenario you just replace a number of elements in the list by the same number of other elements:
>>> lst = [1, 2, 3, 4, 5, 6, 7, 8]
>>> lst[2:6] = ['a', 'b', 'c', 'd'] # 4 items in slice, 4 items assigned
>>> lst
[1, 2, 'a', 'b', 'c', 'd', 7, 8]
You can also replace a number of elements in the list by a smaller number of other elements. As a result, the list will shrink.
>>> lst = [1, 2, 3, 4, 5, 6, 7, 8]
>>> lst[2:6] = 'a' # 4 items in slice, 1 item assigned
>>> lst
[1, 2, 'a', 7, 8]
Naturally, the opposite is also possible. If you replace a number of elements in the list by a greater number of other elements, the list will expand:
>>> lst = [1, 2, 3, 4, 5, 6, 7, 8]
>>> lst[2:6] = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'] # 4 items in slice, 8 items assigned
>>> lst
[1, 2, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 7, 8]
How to Use Slice Assignment to Imitate List Methods
This is more or less it, as far as slice assignment is concerned, but, here’s something for you to practice. Although in a real life scenario we’d use list methods to append, insert or delete list items, let’s see how this can be done with slice assignment.
Using Slice Assignment to Imitate the append Method
Let’s start with appending an item. To append an item is the same as to insert it at the end. Have a look:
# using method
>>> lst = [1, 2, 3, 4, 5, 6, 7, 8]
>>> lst.append('a')
>>> lst
[1, 2, 3, 4, 5, 6, 7, 8, 'a']
# using slice assignment
>>> lst = [1, 2, 3, 4, 5, 6, 7, 8]
>>> lst[len(lst):] = 'a' # empty slice at the end
>>> lst
[1, 2, 3, 4, 5, 6, 7, 8, 'a']
Using Slice Assignment to Imitate the insert Method
Now let’s insert an item at the beginning:
# using method
>>> lst = [1, 2, 3, 4, 5, 6, 7, 8]
>>> lst.insert(0, 'a')
>>> lst
['a', 1, 2, 3, 4, 5, 6, 7, 8]
# using slice assignment
>>> lst = [1, 2, 3, 4, 5, 6, 7, 8]
>>> lst[:0] = 'a' # empty slice at the beginning
>>> lst
['a', 1, 2, 3, 4, 5, 6, 7, 8]
Now let’s insert an item at index 4, so after the number 4:
# using method
>>> lst = [1, 2, 3, 4, 5, 6, 7, 8]
>>> lst.insert(4, 'a')
>>> lst
[1, 2, 3, 4, 'a', 5, 6, 7, 8]
# using slice assignment
>>> lst = [1, 2, 3, 4, 5, 6, 7, 8]
>>> lst[4:4] = 'a' # empty slice inside the list
>>> lst
[1, 2, 3, 4, 'a', 5, 6, 7, 8]
Inserting multiple elements at the end, at the beginning and in the middle is just as simple. Here’s how we can insert 3 items at the beginning using slice assignment:
>>> lst = [1, 2, 3, 4, 5, 6, 7, 8]
>>> lst[:0] = ['a', 'b', 'c']
>>> lst
['a', 'b', 'c', 1, 2, 3, 4, 5, 6, 7, 8]
Using Slice Assignment to Delete List Items
Finally, how can you delete an item? You just need to assign an empty list to a slice. Suppose we want to delete the element at index 3, which is the number 4:
# using the del statement
>>> lst = [1, 2, 3, 4, 5, 6, 7, 8]
>>> del lst[3] # using the del statement
>>> lst
[1, 2, 3, 5, 6, 7, 8]
# using the pop method
>>> lst = [1, 2, 3, 4, 5, 6, 7, 8]
>>> lst.pop(3) # the element is removed and also returned
4
>>> lst
[1, 2, 3, 5, 6, 7, 8]
# using the remove method
>>> lst = [1, 2, 3, 4, 5, 6, 7, 8]
>>> lst.remove(4) # here we pass the element to remove, not the index
>>> lst
[1, 2, 3, 5, 6, 7, 8]
##############################
# using slice assignment
>>> lst = [1, 2, 3, 4, 5, 6, 7, 8]
>>> lst[3:4] = [] # empty list assigned to the slice from index 3 to index 3, so at index 3 exactly
>>> lst
[1, 2, 3, 5, 6, 7, 8]