Concepts / Indexing and Slicing Collections

Indexing and Slicing Collections

Tuples are immutable collections created using comma-separated values, optionally enclosed in parentheses. Once created, their elements cannot be modified.

  • Programming
Interactive lab

Try it: Indexing and Slicing

How Python indexes a sequence from 0 (or from the end with negative numbers) and how a slice start:stop:step picks positions — with the exact CPython rules for omitted and out-of-range bounds.

How it works

  1. Positions run 0 … len−1; negative positions count back from the end (−1 is the last).
  2. An index outside the string raises IndexError.
  3. A slice resolves start and stop (omitted → the ends; out of range → clamped), then walks by step, stopping before stop.
  4. A negative step walks backwards; a step of 0 is a ValueError.

Default run (7 steps): 'PYTHONIC'[1:6:2]: a slice copies characters from start up to (not including) stop, moving by step. … 'PYTHONIC'[1:6:2] → 'YHN' (positions 1, 3, 5).

Simplified: Works on a short string (up to 16 printable ASCII characters); lists slice by exactly the same rules.

Educational simulation

Loading the simulation…

A Collection That Resists Change

A tuple is a collection whose elements cannot be modified after the tuple is created. This makes tuples useful when a group of values should remain fixed. Python also lets you access tuple elements by index, just as you access elements in a list.

Key point

The central distinction is simple: tuples protect their elements from modification, while lists allow their elements to be modified.

Reading Values by Position

Indexing means accessing an element by its position. Tuples support indexing in the same way as lists. In the generated example below, the value at index 0 is the first value, and the value at index 1 is the second value.

Tuple("red", "green", "blue")Index 0redIndex 1greenIndex 2blueindex 0index 1index 2
Which value is reached when a tuple is accessed by a particular index?
Worked example

Following an index

Determine which value is selected from the tuple ("red", "green", "blue") at index 1.

  1. 1
    Start with the tuple

    The tuple contains three ordered values: red, green, and blue.

  2. 2
    Use index 1

    Indexing selects the value stored at position 1.

  3. 3
    Read the selected value

    The value associated with index 1 is green.

Result

The access selects green.

Creating Tuple Values

Definition

Tuples are created with comma-separated values. Parentheses may enclose those values, but the defining pattern described in the source is the comma-separated values themselves.

python

Both generated examples use comma-separated values. The first leaves out parentheses; the second includes them. In either case, the values are presented as a tuple according to the creation rule supplied by the source.

Reaching Through Nested Tuples

A tuple can be nested inside another tuple. This creates a hierarchical structure: an outer index first selects one item from the outer tuple, and that selected item can itself be accessed by another index if it is an inner tuple.

Outer tuple(("Ada", 36), ("Lin", 29))Index 0LinIndex 1("Lin", 29)Index 129outer index 1inner index 0inner index 1
How do two index positions map from an outer tuple to a value inside an inner tuple?
Worked example

Tracing two indexes

Find the value reached by selecting index 1 from the outer tuple and then index 0 from the selected inner tuple.

  1. 1
    Select the outer item

    Index 1 selects the inner tuple ("Lin", 29).

  2. 2
    Select the inner item

    Index 0 of the selected inner tuple is Lin.

  3. 3
    Combine the access steps

    The first index chooses the inner tuple; the second index chooses a value inside it.

Result

The nested access reaches Lin.

Protecting Fixed Data

Tuple immutability means that once a tuple has been created, its elements cannot be modified. A list has the opposite behavior described in the source: its elements allow full modification. Choose a tuple when you need to guarantee that a group of values will not change, including as a function return value or a dictionary key.

Tuple(10, 20)Element replacementattemptedTuple(10, 20)List[10, 20]Element replacementattemptedList[10, 99]replace elementmodification not allowedreplace elementmodification allowed
What happens when an element replacement is attempted in a tuple compared with a list?

What do you think happens?

A tuple contains (10, 20), and code attempts to replace the value at one index. What should you predict?

  • The tuple is updated

  • The tuple remains protected because its elements cannot be modified

  • The tuple becomes a list

Reveal answer
Answer:

The tuple remains protected because its elements cannot be modified.

Immutability prevents modification of tuple elements after creation. A list, in contrast, allows its elements to be modified.

python

In this generated example, the tuple element replacement is not allowed because tuple elements cannot be modified. The list element replacement is allowed because lists permit full modification. The important prediction is about whether the collection can change, not about changing the index itself.

Choosing Between Tuples and Lists

CollectionElement accessElement modificationUseful when
TupleAccess by index, like a listNot allowed after creationData must be guaranteed not to change
ListAccess by indexFull modification is allowedData needs to remain changeable

The source-supported distinction between tuples and lists

Best practice

Use a tuple when protecting the values is part of the design. The source specifically identifies function return values and dictionary keys as situations where a tuple can help guarantee that the data will not change.

Mistakes with Tuple Access

  • Common mistake

    Assuming parentheses alone are the important part of tuple creation.

    What it looks like
    coordinates = 4, 9
    Why it fails

    The source describes tuples as comma-separated values with optional parentheses.

    Do this instead

    Focus on the comma-separated-value pattern; parentheses may be included but are optional in the stated tuple syntax.

  • Common mistake

    Treating a tuple like a list that can be updated.

    What it looks like
    fixed_values = (10, 20), followed by an attempt to replace one element
    Why it fails

    Tuple elements cannot be modified after the tuple is created.

    Do this instead

    Use a list when the elements need full modification, or use a tuple when the values should remain fixed.

  • Common mistake

    Stopping after the outer index when working with nested tuples.

    What it looks like

    Selecting an inner tuple but not selecting the value inside it

    Why it fails

    A nested tuple is itself a tuple, so reaching an inner value requires another index.

    Do this instead

    Trace the access in stages: first select the inner tuple from the outer tuple, then select an element from that inner tuple.

  • Common mistake

    Assuming indexing changes the collection.

    What it looks like

    Reading one tuple element and expecting the tuple to be rewritten

    Why it fails

    Indexing accesses an element; immutability concerns attempts to modify an element.

    Do this instead

    Separate read operations from modification attempts. Tuple elements can be accessed, but they cannot be modified.

Practice the Trace

MEDIUM

For the nested tuple (("Ada", 36), ("Lin", 29)), trace the access that selects the second inner tuple and then selects its first value. State the value reached and explain why attempting to replace that value would not modify the tuple.

Hints
  • Begin with the index in the outer tuple.
  • The selected outer item is itself a tuple.
  • Use a second index to select the value inside that inner tuple.
  • Apply the immutability rule to the attempted replacement.
EASY

Decide whether each collection is a better fit for a tuple or a list: values that must not change, and values that need full modification. Explain your choice using immutability.

Hints
  • A tuple protects its elements from modification.
  • A list allows full modification.
  • The source identifies function return values and dictionary keys as tuple use cases.

Key Takeaways

  • Tuples are created from comma-separated values, with optional parentheses.
  • Tuple elements can be accessed by index just like list elements.
  • Nested tuples require tracing the outer index and then the inner index.
  • Tuples are immutable, so their elements cannot be modified after creation.
  • Use tuples for data that must remain fixed, and use lists when full modification is required.

Related concepts

More concepts starting with I