What Is [::-1] in Python?

What does [::-1] do in Python

In Python, [::-1] means reversing a string, list, or any iterable with an ordering.

For example:

hello = "Hello world"
nums = [1, 2, 3, 4]

print(hello[::-1])
print(nums[::-1])

Output:

dlrow olleH
[4, 3, 2, 1]

This is the quick answer.

However, reversing a list in Python used to be something I always had to look up as I never wanted to learn how it really works. But now that I understand the mechanism, it is a no-brainer.

To truly understand how [::-1] works in Python, you need to understand what is slicing in Python. And before you can understand what slicing is, you need to understand what is indexing and especially negative indexing.

Indexing in Python

To access an element in a Python iterable, such as a list, you need to use an index that corresponds to the position of the element.

In Python, indexing is zero-based. This means the first element has 0 as its index, the second element has 1 as its index, and so on.

Let’s demonstrate indexing with lists. Here we have a list of numbers from which we want to access the 1st and 2nd elements. To do this, we need to use indexes 0 and 1 in square brackets to access the values from the list.

nums = [1, 2, 3, 4]

first = nums[0]
second = nums[1]

print(first, second)

Output:

1 2

The same indexing works with other iterables, such as strings, tuples, and dictionaries.

For example, let’s access the characters of a string with indexes:

word = "Hello"

first = word[0]
second = word[1]

print(first, second)

Output:

H e

Negative Indexing in Python

In Python, it is also possible to use negative indexing to access values of a sequence.

Negative indexing accesses items relative to the end of the sequence. The index -1 reads the last element, -2 the second last, and so on.

For example, let’s read the last and the second last number from a list of numbers:

nums = [1, 2, 3, 4]

last = nums[-1]
second_last = nums[-2]

print(last, second_last)

Output:

4 3

Now you understand how indexing works in Python. Let’s next talk about slicing.

Slicing in Python

In Python, slicing makes it possible to access parts of sequences, such as strings or lists. This makes it possible to access, modify, and delete items in a readable and concise fashion.

Slicing works similar to indexing, but instead of accessing a single value, multiple values are accessed.

Slicing uses indexing to access the range of elements. These indexes are also zero-based.

Slicing Syntax

You can slice iterables in Python in two possible ways:

sequence[start:stop]

This way you access a range of elements beginning at index start and ending one before the stop index. In other words, it returns sequence[start], sequence[end – 1] and everything between.

Another way to slice iterables in Python is by:

sequence[start:stop:step]

This means you get a range of elements from start to stop with a step size of the step. For example, with a step of 2, every second element in that range is returned.

For example, let’s get the 4th, 5th, and 6th elements from a list of numbers:

nums = [1, 2, 3, 4, 5, 6, 7, 8]

part = nums[3:6]

print(part)

Output:

[4, 5, 6]

As another example, let’s get every second value between the 4th and 8th elements of the list. To do this, we also need to specify the step in addition to start and stop.

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

part = nums[3:8:2]

print(part)

Output:

[4, 6, 8]

Negative Slicing in Python

Similar to negative indexing, Python also supports negative slicing. This means you can start slicing from the end of the sequence.

For example, let’s access the last 3 values of a list:

nums = [1, 2, 3, 4, 5, 6, 7]

part = nums[-4:-1]

print(part)

Output:

[4, 5, 6]

Here the slicing starts at the index -4 which is the 4th last element of the list. The slicing ends at the last item, which is at the index -1.

Also, the step size can be a negative value. By doing this, the direction of slicing is reversed. This means the slicing starts from the sequence[stop + 1] and stops at the sequence[start] value.

For example, let’s get the 5th, 4th, and 3rd elements from a list of numbers:

nums = [1, 2, 3, 4, 5, 6, 7]

part = nums[4:1:-1]

print(part)

Output:

[5, 4, 3]

To clarify how this works:

  • The step is negative, so the slicing is reversed.
  • The stop is 1 so the slicing stops at 1+1 = 2nd element.
  • The start is 4, so the slicing starts at the index 4, that is, at the 5th element.

Omit Start and Stop from Slicing

If you want to start slicing from the beginning of the sequence, you can leave out the start value.

For instance, let’s get the first 4 values of a list. To do this, you can leave out the 0 as the start, and only specify 4 as the stop:

nums = [1, 2, 3, 4, 5, 6, 7]

part = nums[:4]

print(part)

Output:

[1, 2, 3, 4]

Also, if you want to slice a sequence from some point all the way up to the end of the sequence, you can omit the stop value.

For instance, let’s get all the values from the 3rd value from a list of numbers. To do this, you only need to specify the start index 2 and leave out the stop:

nums = [1, 2, 3, 4, 5, 6, 7]

part = nums[2:]

print(part)

Output:

[3, 4, 5, 6, 7]

Now, if you want to slice the whole list, that is return the list as is, you can leave out both the start and the stop indexes.

For example:

nums = [1, 2, 3, 4, 5, 6, 7]

part = nums[::]

print(part)

Output:

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

This returns the slice that is the whole list.

Even though omitting start and stop this way is useless, there is a meaningful way you can use it too.

For instance, to get every second value from a list, slice the list without the start and stop values but specify a step:

nums = [1, 2, 3, 4, 5, 6, 7]

part = nums[::2]

print(part)

Output:

[1, 3, 5, 7]

You may see where we are going…

The [::-1] Part

To reverse a list in Python, you can use negative slicing:

  • As you want to slice the whole list, you can omit the start and stop values altogether.
  • To reverse the slicing, specify a negative step value.
  • As you want to include each value in the reversed list, the step size should be -1.

Here is how it looks in code.

nums = [1, 2, 3, 4, 5, 6, 7]

part = nums[::-1]

print(part)

Output:

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

This returns a reversed version of the original list. This is exactly what the [::-1] does!

To recap, [::-1] slices a Python list from the last value to the first value by taking steps of size 1 backward.

(Notice how everything you learned can be used with other Python iterables, such as strings or tuples.)

Conclusion

Today you learned what is [::-1] in Python.

To recap, [::-1] means you create a slice of a sequence that traverses the whole sequence starting from the end of it.

Normally, the slicing follows sequence[start:stop:step] syntax. However, when you want to go from the beginning of the sequence to the end of it, you can omit start and stop. To indicate you want to start slicing from the end and end at the start, specify a negative step.

Further Reading

Python Tricks

How to Write to a File in Python

The with Statement in Python

Share on facebook
Share on twitter
Share on linkedin

Leave a Comment

Your email address will not be published.