How to Slice a List in Python (Indices, Steps, & Reversal)

Learn how to slice lists in Python using the powerful bracket syntax. Master start, stop, and step arguments, negative indexing, and reversing lists.

Try Code in Editor

Explanation

Slicing is one of Python's most intuitive and powerful features, enabling you to extract subsets of a list, tuple, or string with clean and concise syntax. Instead of writing verbose loops to copy specific elements from a list, Python's slice notation lets you specify a range of indices inside square brackets.

The complete syntax for list slicing is `my_list[start:stop:step]`. The `start` index is inclusive, the `stop` index is exclusive, and `step` defines the increment between elements (defaulting to 1). If you omit `start`, Python defaults to the beginning of the list. If you omit `stop`, Python defaults to the end of the list. This makes extracting portions of collections highly expressive.

Slicing is also extremely versatile because it supports negative indices. A negative start or stop index counts backward from the end of the list (where `-1` represents the last item). By setting a negative step value, such as `my_list[::-1]`, you can quickly retrieve a reversed copy of the list. Understanding slice ranges is essential for clean data extraction.

Step-by-Step Implementation

  1. 1

    Use list[start:stop] to extract elements from index start up to, but not including, stop.

  2. 2

    Omit start or stop indices (e.g., list[:4] or list[4:]) to slice from the boundary of the list.

  3. 3

    Pass a third argument inside the bracket list[::step] to skip elements at regular intervals.

  4. 4

    Use negative indices to count elements backward from the end of the collection.

Code Example

This script demonstrates list slicing using different start, stop, step ranges, negative indexing, and list reversal.

list_slicing.py
Try in Editor
numbers = [0, 10, 20, 30, 40, 50, 60, 70, 80]

# 1. Basic slicing: elements from index 2 up to (but excluding) 6
subset = numbers[2:6]
print("Subset [2:6]:", subset)

# 2. Omitting indices: start to index 4, and index 5 to end
first_five = numbers[:5]
last_four = numbers[5:]
print("First five [:5]:", first_five)
print("Last four [5:]:", last_four)

# 3. Using a step parameter: get every second element
every_second = numbers[::2]
print("Every second [::2]:", every_second)

# 4. Negative indexing: get the last three elements
last_three = numbers[-3:]
print("Last three [-3:]:", last_three)

# 5. Reverse the list using step=-1
reversed_numbers = numbers[::-1]
print("Reversed list [::-1]:", reversed_numbers)
Terminal Output
Subset [2:6]: [20, 30, 40, 50]
First five [:5]: [0, 10, 20, 30, 40]
Last four [5:]: [50, 60, 70, 80]
Every second [::2]: [0, 20, 40, 60, 80]
Last three [-3:]: [60, 70, 80]
Reversed list [::-1]: [80, 70, 60, 50, 40, 30, 20, 10, 0]

Frequently Asked Questions

Does slicing modify the original list?

No, list slicing returns a shallow copy of the requested range, leaving the original list entirely unchanged.

What happens if the slice indices are out of bounds?

Unlike accessing a single index which raises an IndexError, slicing is forgiving and simply caps the range at the boundaries of the list, returning an empty list if no elements fall in the range.

Related How-To Guides

Recommended Python Resources

Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.