Concepts / Comparing Tuples and Lists

Comparing Tuples and Lists

Tuples support read-only indexing and slicing operations identical to lists.

  • 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…

The Familiar Access Pattern

Tuples and lists look similar when you read from them. Both support bracket access for an individual position and slicing for a range of positions. The important difference appears when you try to write a new value into an existing position: list contents can be modified, but tuple elements cannot be modified in place.

Tupleread-only accessIndexingreturns one elementIndexingreturns one elementSlicingreturns a tupleListread and modifySlicingreturns a listaccessaccessaccessaccess
Which access operations behave alike, and where does mutability create a difference?

Indexing Versus Slicing

Indexing asks for one position. For a tuple t, t[0] returns the single element at position 0. Slicing asks for a range. For example, t[1:] extracts the range beginning at position 1 and returns a new tuple. Both operations read from the tuple; neither changes it.

tuplea, b, c, d, etuple[0]one elementasingle valuetuple[1:]selected range(b, c, d, e)new tupleindexreturnsslicereturns
What is the difference between tuple[2] and a slice such as tuple[1:4], and what kind of value does each operation return?
OperationWhat it selectsWhat it returns
IndexingOne tuple positionA single element, whatever type that element is
SlicingA selected rangeA tuple

Indexing and slicing are both read-only, but they do not return the same kind of result.

Reading a Slice Range

A slice expression describes a range rather than one position. The source example t[1:] starts at position 1 and extracts the remaining elements, producing ('b', 'c', 'd', 'e'). The result is a tuple, which is why it can participate in tuple concatenation.

0at[1:]starts at 1(b, c, d, e)tuple result1b2c3d4eselected range beginsselectedselectedselectedreturns
Which elements are selected when a slice begins at position 1 and continues through the remaining tuple?
python
Output
'a'
('b', 'c', 'd', 'e')

Why In-Place Assignment Fails

A tuple lets you read from a position, but it does not let you write a new value into that position. An assignment such as t[0] = 'new_value' attempts to modify the existing tuple. Python rejects that operation by raising a TypeError. The original tuple remains unchanged because tuple immutability prevents in-place modification.

t('a', 'b', 'c')t[0] = 'A'in-place writeTypeErrorassignment rejectedt('a', 'b', 'c')attemptraisesoriginal remains
What happens when code tries to assign a new value to an existing tuple position?
python
Output
TypeError

Replacing Rather Than Editing

Worked example

Putting A in the first position

Create a tuple with 'A' in the first position instead of 'a'.

  1. 1
    Keep the tail

    The slice t[1:] extracts ('b', 'c', 'd', 'e'), the part after the first element.

  2. 2
    Make a one-element tuple

    ('A',) represents a tuple containing one element. The comma is needed to make it a tuple rather than a parenthesized string.

  3. 3
    Concatenate

    ('A',) + t[1:] combines the replacement tuple with the extracted tail.

  4. 4
    Rebind the variable

    Assigning the concatenation back to t makes t refer to the fresh tuple. The original tuple was not edited.

Result

t now refers to ('A', 'b', 'c', 'd', 'e').

t('a', 'b', 'c', 'd', 'e')t[1:]('b', 'c', 'd', 'e')t('A', 'b', 'c', 'd', 'e')('A',)one-element tupleextractconcatenateconcatenate
How does concatenation create a modified state without changing the original tuple in place?
python
Output
('A', 'b', 'c', 'd', 'e')

Mistakes with Tuple Operations

  • Common mistake

    Treating tuple indexing like tuple assignment

    What it looks like
    t[0] = 'new_value'
    Why it fails

    Indexing can read one element, but tuple elements cannot be modified in place. The assignment raises a TypeError.

    Do this instead

    Construct a new tuple and rebind the variable.

  • Common mistake

    Confusing an element with a one-element tuple

    What it looks like

    Using 'A' where ('A',) is required for concatenation

    Why it fails

    Indexing returns a single element, while slicing returns a tuple. Concatenation requires tuple operands.

    Do this instead

    Use ('A',) + t[1:] when adding a replacement element to a tuple slice.

  • Common mistake

    Treating t[0:1] and t[0] as interchangeable

    What it looks like

    Using a single-element result where a tuple result is needed

    Why it fails

    Indexing returns one element; slicing returns a tuple. Their result types and uses are different.

    Do this instead

    Choose indexing when you need the element and slicing when you need a tuple range.

  • Common mistake

    Concatenating a string with a tuple

    What it looks like
    'A' + t[1:]
    Why it fails

    The replacement value is a string, while t[1:] is a tuple. A string and a tuple cannot be concatenated in this operation.

    Do this instead

    Wrap the replacement in a one-element tuple: ('A',) + t[1:].

A Safer Data Choice

Immutability is useful when data should remain stable. A function that receives a tuple cannot accidentally modify that tuple. Tuples can also be used as dictionary keys, while lists cannot be keys because lists are mutable. These properties make tuples useful when predictable, unchanged data matters.

Check Your Reasoning

MEDIUM

A tuple t contains ('a', 'b', 'c', 'd', 'e'). Explain the difference between t[0] and t[1:]. Then describe why t[0] = 'A' fails and write the tuple-concatenation pattern that places 'A' first.

Hints
  • Indexing returns one element; slicing returns a tuple.
  • The original tuple cannot be changed in place.
  • Use a one-element tuple for the replacement and combine it with t[1:].

What do you think happens?

What kind of result does t[1:] produce when t is a tuple?

  • One element

  • A new tuple

  • A modified original tuple

Reveal answer
Answer:

A new tuple

Tuple slicing returns a tuple containing the selected range, and the read operation does not modify the original tuple.

Key Takeaways

  1. 1
    Tuples and lists support comparable indexing and slicing access operations.
  2. 2
    Indexing returns one element, while slicing returns a tuple.
  3. 3
    Tuple access is read-only; assigning to a tuple position raises a TypeError.
  4. 4
    To change the contents conceptually, build a new tuple with slicing and concatenation, then rebind the variable.
  5. 5
    Tuple immutability provides stable data that functions cannot accidentally modify.

At a glance

  • Tuple indexing and slicing resemble list indexing and slicing, but tuple access is read-only.
  • Indexing returns a single element; slicing returns a tuple containing a selected range.
  • In-place assignment to a tuple position raises a TypeError.
  • A modified state is achieved by creating a new tuple with slicing and concatenation, then rebinding the variable.
  • Tuple immutability supports predictable data and prevents accidental modification.

Related concepts

More concepts starting with C