← Back to list

A surprising property of Python list slicing with indices

If you’re working with or learning Python, you probably know that lists are mutable, ordered collections of data that can hold elements of…

Jasenko Krejić · 2025-09-25 08:08 · 0 claps · 2.4 min read
#python #python-list #python3 #python-programming
Open on Medium ↗
Wiki topics: EDU · Education & Learning 💻 · Programming

A surprising property of Python list slicing with indices

If you’re working with or learning Python, you probably know that lists are mutable, ordered collections of data that can hold elements of different types. For now, let’s set aside their mutability and their ability to store multiple data types, and focus instead on their ordered nature. To illustrate this, let’s create a simple list.

lst = [0, 1, 2, 3, 4, 5, 6]

The beauty of lists is that they can be indexed — meaning we can access individual elements by their position, starting from zero on the left. For example, if we want to print the number 2, which is the element at index 2, we would write:

print(lst[2])
2

But what happens if we try to print an element at an index that’s out of range — for example, index 7?

print(lst[8])
IndexError: list index out of range

This will raise an IndexError exception, which immediately terminates the program. To handle this, we can use a try…except block if we expect such an error to occur, or we can first check whether the index is valid by using the len() function to ensure it falls within the allowed range.

You’ve also probably used list slicing — a way to extract a sublist from an existing list. Slicing returns elements between the start index (inclusive) and the end index (exclusive). These indices are separated by a colon (:). For example, the following expression extracts elements 2, 3, and 4:

slice = lst[2:5]
print(slice)
[2, 3, 4]

Note that element 5 (at index 5) is not included. Also, the original list remains unchanged.

Now, in this list lst, there’s no element at index 8. What do you think will happen if we try this:

slice = lst[2:8]
print(slice)

Think about it. Will we get an IndexError? Or an empty list — no exception, but a hint that something didn’t go as expected? Run the program and see.

[2, 3, 4, 5, 6]

We didn’t get an error, and we didn’t get an empty list either. Instead, Python simply ignored the fact that there’s no element at position 8 (or even 7) and returned only the elements that actually exist within the specified range. What a relief! Here’s an illustration to demonstrate this:

This also applies when we specify a step (the third option in the colon-separated slicing syntax) . For example:

slice = lst[2:8:10]
print(slice)
[2]

But be careful — if you create a slice like this:

slice = lst[8:2]
print(slice)
[]

It returns an empty list, because this is a different situation: we’re not allowed to use a start index that’s larger than the end index — unless we specify a negative step as the third parameter. Without it, the result is simply an empty list.

These small details are very common in Python, and they can make or break your program — not to mention your day — if you let them pile up without taking the time to understand them thoroughly.

And yes, since tuples and strings use the same indexing and slicing as lists do, everything I mentioned about lists, can be applied to tuples and strings when you slice them with non-existing indices.


메타데이터
post_id
84da3be90643
slug
a-surprising-property-of-python-list-slicing-with-indices-84da3be90643
url
https://medium.com/@krejjas/a-surprising-property-of-python-list-slicing-with-indices-84da3be90643
canonical_url
https://medium.com/@krejjas/a-surprising-property-of-python-list-slicing-with-indices-84da3be90643
author_url
https://medium.com/@krejjas
status
ok
fetched_at
2026-06-24 11:06:28