Software, Tech & Coding simplified.

Python Slice Notation: Quick Explanation

In Python, slicing is used to access specific parts of an iterable, such as a list.

Here are some examples:

arr = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"]

# The last element
arr[-1]      # "J"

# Last three elements
arr[-3:]     # ["H", "I", "J"]

# Everything except the last three elements
arr[:-3]     # ["A", "B", "C", "D", "E", "F", "G"]

# From 2nd element to 8th with 3 step interval
arr[1:8:3]   # ["B", E", H"]

# First three elements reversed
arr[2::-1]   # ["C", "B", "A"]

# Last three elemenets reversed
arr[:-4:-1]  # ["J", "I", "H"]

# Everything except last threee reversed
arr[-4::-1]  # ["G", "F", "E", "D", "C", "B", "A"]

# The whole list reversed
arr[::-1]    # ["J", "I", "H", G", "F", "E", "D", "C", "B", "A"]

But how does the slicing notation work?

There are two syntaxes you can use the slice notation in Python:

  • [start:stop]
  • [start:stop:stepsize]

Let’s take a look at how both of these work.

[start:stop]

The slicing notation [start:stop] means the array is sliced from start index and stops at stop – 1. So the stop specifies the first value that is not in the selected range!

You can also leave these parameters out:

  • Leaving start blank means the slice starts at the first element.
  • Leaving end blank means the slice ends to the last element.
a[start:stop]  # elements from start to stop-1
a[start:]      # elements start to the end
a[:stop]       # elements from the beginning to stop-1
a[:]           # elements from first element to last (copies the iterable).

Examples:

arr = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"]

arr[2:5]  # ["C", "D", "E"]
arr[5:]   # ["F", "G", "H", "I", "J"]
arr[:5]   # ["A", "B", "C", "D", "E"]

The start and stop values can also be negative.

  • If start is negative, the counting starts from the end.
  • If stop is negative, the counting stops with respect to the end.
arr = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"]

arr[-3:]     # ["H", "I", "J"]
arr[:-3]     # ["A", "B", "C", "D", "E", "F", "G"]

[start:stop:stepsize]

Another way to slice iterables in Python is by specifying a third argument, stepsize, to the slicing syntax.

[start:stop:stepsize]

Otherwise, this works the same way as the [start:stop] syntax. The only difference is that the stepsize now specifies how many elements to jump over when slicing.

Examples:

arr = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"]

# Every third element from index 1 to 8.
arr[1:8:3]   # ["B", E", H"]

# Every second element through out the list
arr[::2]     # ["A", "C", "E", "G", "I"]

Also, the stepsize can be negative.

In this case, the slicing direction is reversed.

Examples:

arr = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"]

arr[2::-1]   # ["C", "B", "A"]
arr[:-4:-1]  # ["J", "I", "H"]
arr[-4::-1]  # ["G", "F", "E", "D", "C", "B", "A"]
arr[::-1]    # ["J", "I", "H", G", "F", "E", "D", "C", "B", "A"]

What Is a Slice Object?

The [] slicing operator is related to a slice object in Python.

Here is an illustration of how using [] and slice objects relate to one another in different situations.

[start:stop]      <==> slice(start, stop)
[start:]          <==> slice(start, None)
[:stop]           <==> slice(None, stop)

[start:stop:step] <==> slice(start, stop, step)
[start::step]     <==> slice(start, None, step)
[::step]          <==> slice(None, None, step)

As you can see, if you use slice(), you cannot leave the parameters empty. Instead, use None.

Other than that, the slice() object works with the same idea as the [] operator.

For example:

arr = ["A", "B", "C", "D", "E"]

arr[:2]                    # ["A", "B"]
arr[slice(None, 2)]        # ["A", "B"]

arr[::-1]                  # ["E", "D", "C", "B", "A"]
arr[slice(None, None, -1)] # ["E", "D", "C", "B", "A"]

Thanks for reading.

If you are looking for a more complete tutorial, feel free to check this article.

Further Reading

Slicing in Python: Comprehensive Guide

50 Sites to Learn Coding

50 Swift Interview Questions

Share

Share on twitter
Share on linkedin
Share on facebook
Share on pinterest
Share on email