Skip to content
Home » Slice Assignment in Python – How Does It Work?

Slice Assignment in Python – How Does It Work?

Spread the love

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.

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.

slice assignment in Python

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]

Your Panda3D Magazine

Make Awesome Games and Other 3D Apps

with Panda3D and Blender using Python.

Cool stuff, easy to follow articles.

Get the magazine here (PDF).

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']

Python Jumpstart Course

Learn the basics of Python, including OOP.

with lots of exercises, easy to follow

The course is available on Udemy.

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]

Blender Jumpstart Course

Learn the basics of 3D modeling in Blender.

step-by-step, easy to follow, visually rich

The course is available on Udemy and on Skillshare.

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] 


Spread the love

Leave a Reply