✦ Python Foundations Collections module

Concept 07 · Indexing & Slicing

Find one item.
Take a useful slice.

Indexes select one list item; slices select an ordered part of a list.

01 Learn02 Try03 Predict

Learning as a guest

Learning as a guest? Save your progress!

Create a free account to learn with Sloby AI, check your understanding, and track your progress.

The entire platform is free throughout 2026. Foundations stays free forever.

An index selects one position. A slice selects an ordered part.

In Lists, you learned that every item has a position. Indexing lets you select one of those positions. Slicing lets you select a range of positions and gives you a new list containing the selected values. Think: index → one position → one value; slice → range of positions → a new list.

numbers = [10, 20, 30, 40, 50]

print(numbers[2])

Python counts list positions from 0: numbers[0] is 10, numbers[1] is 20, and numbers[4] is 50. In numbers[2], the 2 is a position, not the value you want. Follow the path: numbers → index 2 → value 30.

30

Positive indexes count from the beginning. Negative indexes count from the end. A slice uses start:stop:step: its start position is included, but its stop boundary is not included.

↳

A useful map: values:   10   20   30   40   50
positive indexes: 0    1    2    3    4
negative indexes: -5   -4   -3   -2   -1

Negative indexes count from the end
numbers = [10, 20, 30, 40, 50]

print(numbers[-1])
print(numbers[-2])

-1 selects the last item, so it prints 50. -2 selects the item before the last, so it prints 40. Think: -1 → last item; -2 → one item before the last.

Indexing chooses one value; slicing creates a new list
numbers = [10, 20, 30, 40, 50]

print(numbers[2])
print(numbers[1:4])

numbers[2] selects one position and gives you one value: 30. numbers[1:4] selects an ordered range of positions and gives you a new list: [20, 30, 40]. Indexing is for one item; slicing is for a selected part of the list.

Start is included; stop is excluded
numbers = [10, 20, 30, 40, 50]

print(numbers[1:4])

Read numbers[1:4] as “start at index 1 and stop before index 4.” Python takes positions 1, 2, and 3, which hold 20, 30, and 40. It does not take the value at index 4. This makes the slice length predictable: 4 - 1 = 3 selected elements.

Omit start to begin at the first item
numbers = [10, 20, 30, 40, 50]

print(numbers[:3])

With no start boundary, numbers[:3] begins at the first item and stops before index 3. The result is [10, 20, 30].

Omit stop to continue to the end
numbers = [10, 20, 30, 40, 50]

print(numbers[2:])

With no stop boundary, numbers[2:] starts at index 2 and continues to the end. The result is [30, 40, 50]. With neither boundary, numbers[:] selects the whole list and produces a new list containing all its items.

Use step to take every second item
numbers = [10, 20, 30, 40, 50]

print(numbers[::2])

The three slice parts are start:stop:step. Here start and stop are omitted, so Python uses the whole list. A step of 2 takes every second item: [10, 30, 50]. Step 1 means every item; step 2 means every second item.

A missing index and a wide slice behave differently
numbers = [10, 20, 30]

# numbers[10] raises IndexError
print(numbers[:10])

Indexing asks for one exact position, so numbers[10] raises IndexError when that position does not exist. A slice can safely extend beyond the available positions: numbers[:10] simply takes the available items and produces [10, 20, 30].

Trace indexes and boundaries before you run
scores = [12, 24, 36, 48, 60]

print(scores[2])
print(scores[-1])
print(scores[1:4])
print(scores[:2])

Before running this code, predict four results: the value at index 2, the final value, the positions included by [1:4], and the result of [:2]. The outputs are 36, 60, [24, 36, 48], and [12, 24]. Trace positions and boundaries instead of guessing.

Common beginner mistakes

  • Treating the index as the valueFor numbers = [10, 20, 30], numbers[1] does not ask for the number 1. The 1 is a position, and the selected value is 20.
  • Including the stop positionnumbers[1:4] does not include the item at index 4. Keep the rule simple: start included; stop excluded.
  • Thinking -1 means the first item-1 means the last item. Negative indexes count backward from the end of the list.
  • Expecting an index and a slice to handle missing positions the same wayA missing index can raise IndexError; a slice that extends beyond the list simply uses the items that exist.

Python Playground

Loading Python runtime…

Before running the code, predict every result. Decide whether each line uses an index or a slice; for a slice, name its start and stop boundary. Then change one boundary, try a different negative index, change the step, and compare the output with your prediction.

indexing_slicing.pyPython
OutputWaiting for Python
The browser runtime is loading. You can still edit the example code.

Pyodide runs Python in a browser Web Worker. It is stopped and restarted if code keeps running. Browser execution is not a complete security boundary; do not enter secrets or rely on it for untrusted code isolation.

What will this program print?

The stop index is not included in a slice.

letters = ["a", "b", "c", "d", "e"]
print(letters[1:4])
Choose the output
Sloby, the Python AI Tutor mascot

Your learning mentor

Sign in to ask Sloby

Create a free account or sign in to get AI guidance while you learn.

Sign in to check your understanding

Create a free account or sign in to receive AI feedback and save your learning progress.

Python AI Tutor · Learn by understanding, not just by running code.