Lesson 17: Slicing

Slicing is yet another super practical aspect of Python. It makes accessing strings, lists and tuples partially very effortless.

There are a few rules you need to follow while slicing data in Python and it will be beneficial for you to learn these by heart. Repeat as much as needed to gain confidence in Python Slicing.

Let’s say you have a list: lst = [1,2,3,4,5]

Ex. 1:
If you slice it as: lst[1:3]
You will get: [2,3]
Index starts at zero, start is inclusive but stop is exclusive.

Function : N/A

No new function will be introduced in this lesson.

Used Where?

Python’s slicing notation is beautiful and super practical. It can be useful in tons of applications. For instance:
  • Accessing substrings in strings. (email address or domain extraction can be an example, but examples are infinite)
  • Extracting parts of lists
  • Extracting parts of tuples.

Syntax do(s)

1) Follow the syntax notation [start:end:step]

2) If start is not given it will start from 0 (zero)

3) If end is not given it will go all the way to the end

4) If step is not given, steps will increase by 1

5) Slicing always uses brackets regardless of what we are slicing (can be strings, lists or tuples): []

Syntax don't(s)

1) Dictionaries have no orders and their values are accessed by keys. So you can't slice a dictionary.

Example 1

>>> lst = [40,50,20,30,90]
>>> a = lst[1:3]
>>> print(a
)

[50,20]

Notice how ending is exclusive, index starts from 0 and step is 1 since it’s not specified.

Example 2

Remember range() function? and then list() function… Feel free to revisit their lessons if you need to fresh up. But just like slicing Range function is exclusive with the stop parameter so below example will create a list from 0 to 9 (10 excluded).

>>> lst = list(range(30,100,10))
>>> print(lst)
>>> print(lst[2:6])

[30, 40, 50, 60, 70, 80, 90]
[50, 60, 70, 80]

Example 3

Let’s use a step as well

>>> lst = list(range(10))
>>> print(lst)
>>> print(lst[2:6:2])

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[2, 4]

Example 4

Let’s use a different step

>>> lst = list(range(10))
>>> print(lst)
>>> print(lst[0:8:3])

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 3, 6]

Tips

1- So as we’ve seen, regular slicing syntax is [start:stop:step].

2- However, you can also omit start and stop steps before the colons.

3- [::2]
For instance, when you do this it means it will start from the first element and go all the way to the last element inclusively. And step in this case is 2.

4- [::] In this case, step is not specified so it will be 1 and slicing will start from the first element and go all the way to the end.

5- [:5:2] In this example, step is 2, start is not specified so it will start from the beginning and end is index 5.

6- [3::2] You might want to do it the other way and not specify the stop parameter. Slicing will start from index 3 and go all the way to the end with steps of 2.

Once you understand this properly you can evolve it to any combination that suits your needs.

Let’s see some examples that will reinforce your learning.

Example 5

Below example will give everything until last 2 elements

>>> a = list(range(10))
>>> print(a[:-2])

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

First colon doesn’t have a number in front of it, meaning it will start from the beginning.

Second value is -2, meaning it will stop at second element from the end (exclusive).

There is no third value so, step value is 1 by default.

There is no colon after -2 because, there is no third parameter.

Example 6

And everything until last 2 elements with steps of 2

>>> a = list(range(10))
>>> print(a[:-2:2])

[0, 2, 4, 6]

Example 7

Everything start to finish with steps of 4

>>> a = list(range(10))
>>> print(a[::4])

[0, 4, 8]

First value, is colon : without value, it means start from beginning.

Second value, same. No number in front of the colon :, it means go all the way to the end.

Finally third value is 4, which stands for the step.

Example 8

From 3rd element to the second from last

>>> a = list(range(10))
>>> print(a[2:-2])

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

Example 9

Just to demonstrate that slicing works the same with strings:

>>> a = “Stuttgart”
>>> print(a[2:-2])

‘uttga’

First value 2, stands for start from the index 2 (which is 3rd letter.

Second value -2, stands for stop value, meaning stop at second from the last.

There is no third value, so step is default 1.

Example 10

Only the last two characters:

>>> a = “Stuttgart”
>>> print(a[-2:])

‘rt’

First value -2, stands for start parameter, so it starts from the second from the last since it’s negative.

Second value :, stands for stop parameter, since it’s : only, it means all the way.

There is no third parameter, so step is by default 1.

Slicing vs Indexing

It’s important to note that you will get a list when you slice a list.

But when you index a list you will get the element itself.

Let’s see it in an example:

Example 11

Slicing a list:

>>> a = list(range(10))
>>> print(a[2:3])

[2]

Example 12

Slicing a list:

>>> a = list(range(10))
>>> print(a[2])

2

Example 13

Slicing a list:

>>> b = [“M”, “a”, “r”, “s”]
>>> print(a[2:3])

[‘r’]

Footnote: Single quote : ‘ ‘ and double quote “” have the same function in Python and can be used interchangeably with string representation.

Example 14

Slicing a list:

>>> b = [“M”, “a”, “r”, “s”]
>>> print(a[2])

‘r’

Advanced Concepts (Optional)

Slicing also works decrementally with a negative step. It will start counting from the stop value and backwards until the start value

>>> sequence[high::-step] It starts from high value and goes to index 0
>>> sequence[:low:-step] It starts from last value and goes until low value (exclusive)
>>> sequence[high:low:-step]
It starts from high value and goes until low value (exclusive)
>>> sequence[::-step]
It starts from end and goes until the beginning

Let’s see some examples.

Example 15

Counting from end to the beginning with steps of -1:

>>> a = list(range(10))
>>> print(a[::-1])

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

Example 16

Counting from end to the beginning with steps of -2:

>>> a = list(range(10))
>>> print(a[::-2])

[9, 7, 5, 3, 1]

Example 17

Counting from end the beginning with steps of -2:

>>> a = list(range(10))
>>> print(a[:-2:-2])

[9, 7, 5, 3, 1]

Example 18

Counting from end the beginning with steps of -2:

>>> a = list(range(10))
>>> print(a[:-2:-2])

[9, 7, 5, 3, 1]

Example 19

Counting from end the beginning with steps of -2:

>>> a = list(range(10))
>>> print(a[:-2:-2])

[9, 7, 5, 3, 1]

Next Lesson: Python Operators