PythonArraysBeginner

List slicing [start:end:step]

Extracts a sub-list using start/end indexes and optional step.

Review the syntaxStudy the examplesOpen the coding app
subset = items[start:end:step]

This static page keeps the syntax and examples indexed for search, while the coding app handles interactive exploration and saved references.

What it does

Overview

Extracts a sub-list using start/end indexes and optional step.

List slicing in Python is a robust mechanism for extracting a shallow copy of a sub-sequence. The syntax [start:end:step] selects elements from the 'start' index up to, but not including, the 'end' index. Python handles out-of-bounds indices gracefully in slices; if 'end' exceeds the list length, it simply returns all elements until the end, rather than raising an IndexError. Performance is O(k) where k is the length of the slice, as it requires allocating a new list object and copying the references. Negative indices allow counting from the end of the sequence (e.g., -1 is the last item). Using a negative 'step' reverses the direction of the extraction, which is an idiomatic way to reverse a list.

Quick reference

Syntax

subset = items[start:end:step]

Inputs

Parameters

start (optional)int · Start index (inclusive).
end (optional)int · End index (exclusive).
step (optional)int · Step size (can be negative).

See it in practice

Examples

1

Basic Range Extraction

fruits = ['apple', 'banana', 'cherry', 'date', 'elderberry']
subset = fruits[1:4]
Output:
['banana', 'cherry', 'date']

Extracts elements starting at index 1 up to (but excluding) index 4.

2

Stepping and Skipping

numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
even_numbers = numbers[::2]
Output:
[0, 2, 4, 6, 8]

Uses the step parameter to select every second element from the entire list.

3

List Reversal with Negative Step

chars = ['a', 'b', 'c', 'd']
reversed_chars = chars[::-1]
Output:
['d', 'c', 'b', 'a']

A common Python idiom for creating a reversed copy of a list using a step of -1.

Debug faster

Common Errors

1

LogicError

Cause: Confusing the 'end' index as being inclusive rather than exclusive.

Fix: Remember that the element at the 'end' index is not included in the result. Add 1 to the desired end position.

items = [1, 2, 3]
# Expected [1, 2], but items[0:1] returns [1]
wrong_slice = items[0:1]
2

LogicError

Cause: Attempting to slice with a step of 0, which is undefined.

Fix: Ensure the step value is a non-zero integer.

items = [1, 2, 3]
# Raises ValueError: slice step cannot be zero
bad_slice = items[::0]

Runtime support

Compatibility

Python 3.8+

Source: MDN Web Docs

Common questions

Frequently Asked Questions

Extracts a sub-list using start/end indexes and optional step.

start: Start index (inclusive). end: End index (exclusive). step: Step size (can be negative).

LogicError: Remember that the element at the 'end' index is not included in the result. Add 1 to the desired end position. LogicError: Ensure the step value is a non-zero integer.