3 Minutes Python | Slice

Jay Shi
One Bit At A Time
Published in
3 min readDec 31, 2019

What is slice and how we can use it to handle list/tuple/string?

Photo by timothy muza on Unsplash

What is slice?

Slice is one of the most common operations in python. It can be applied on Tuple, List or String. Its common form is [start:stop:step]. Start defaults to be 0, stop defaults to be the length of the list/tuple/string, and step defaults to be 1.

Note that slice operation does not do in-place change, meaning that it will not change the object on which it is applied.

Examples:

1. Slice can be used to get part of a list/tuple/string

example_lst = [1, 2, 3, 4, 5]
example_str = 'abcde'
example_tupl = (1, 2, 3, 4, 5)
# new_lst_1 now evaluates as [2, 3, 4, 5]
new_lst_1 = example_lst[1:]
# new_lst_2 now evaluates as [1, 2, 3]
new_lst_2 = example_lst[:3]
# new_lst_3 now evaluates as [2, 3]
new_lst_3 = example_lst[1:3]
# new_str now evaluates as 'bc'
new_str = example_str[1:3]
# new_tupl now evaluates as (2, 3)
new_tupl = example_tupl[1:3]

2. Slice can be used to get a reversed list/tuple/string

example_lst = [1, 2, 3, 4, 5]# new_lst now evaluates as [5, 4, 3, 2, 1]
new_lst = example_lst[::-1]

3. Slice can be used to copy a new list/string/tuple. This can be useful since it’s doing a deep copy of the list. This means with the new copied list, we can change it without affecting the old list.

example_lst = [1, 2, 3, 4, 5]# new_lst now evalutes as [1, 2, 3, 4, 5]
new_lst = example_lst[:]
# example_lst is still [1, 2, 3, 4, 5]
new_lst[0] = -99
# lst_ref now also evaluates as [1, 2, 3, 4, 5]
lst_ref = example_lst
# example_lst now changes to [-99, 2, 3, 4, 5]
lst_ref[0] = -99

4. Slice can be used to get part of a list on an interval

example_lst = [1, 2, 3, 4, 5, 6, 7]# new_lst now evaluates as [1, 3, 5, 7]. It will starts from the
# first element at position 0, and then get the element at
# position 0+2 = 2, and then stops at the last element before
# position 7.
new_lst = example_lst[0:7:2]

5. Slice operation returns another list/tuple/string corresponding to the type that it is applied to. And thus, we can do an operation chain on the sliced object.

example_lst = [1, 2, 3, 4, 5, 6, 7]# new_lst now evaluates as [5, 4, 3, 2]. The following operation
# first sliced [1:5], and it returns [2, 3, 4, 5]. And then
# it is applied reverse [::-1], which is essentially
# [2, 3, 4, 5][::-1], and this gives us [5, 4, 3, 2].
new_lst = example_lst[1:5][::-1]

RECAP

This post summarized some functionalities of python slice. Slice is usually in the form of [start:stop:step]. It’s used on list/tuple/string. The operation returns another list/tuple/string and does not change the original object.

We can use slice to get part of list/string/tuple or to reverse it. We can also do a chain slice operation on a list/string/tuple as well.

--

--

Jay Shi
One Bit At A Time

Software Engineer @ Google. I write about Python tutorials and stuff that can help you become a better software engineer.