Concepts / Comparing and Sorting Data Structures

Comparing and Sorting Data Structures

Tuples support equality and ordering comparisons.

  • Programming

A Sequence That Locks Its Contents

Tuples look similar to lists because both are sequences whose elements are accessed by integer positions. The important difference is what happens after creation. A list is flexible: its items can be added, removed, or modified. A tuple is fixed: once it exists, its elements cannot be changed. This makes a tuple useful when a group of related values should stay together and should not be accidentally modified.

Definition

A tuple is an immutable sequence indexed by integers. Immutable means that the tuple's elements cannot be changed after the tuple has been created.

Think of a tuple as an ordered record whose positions are permanent. The order matters, and each value remains associated with its position. This fixed structure is useful for related data such as coordinates or RGB color values, where changing one part accidentally could create a bug.

Mapping Positions to Values

Tupleordered sequence0first element1second element2third element-1last elementbracket accessbracket accessbracket accessbracket access
How does each integer index map to the corresponding element in a tuple?

Tuple access follows the same integer-index idea used with lists. The bracket operator selects an element by position. Index 0 refers to the first element. Positive indices continue from the beginning, while index -1 refers to the last element. The index does not describe the value itself; it describes where that value appears in the ordered sequence.

Worked example

Reading fixed-position data

Imagine a tuple containing the three values north, east, and south in that order. Which value is selected by index 0, and which value is selected by index -1?

  1. 1
    Find index 0

    Index 0 selects the first element in the tuple, so it selects north.

  2. 2
    Find index -1

    Index -1 selects the last element in the tuple, so it selects south.

  3. 3
    Preserve the order

    The values remain in their original order. Accessing an element does not rearrange the tuple.

Result

Index 0 selects north, and index -1 selects south.

What Immutability Prevents

After a tuple is created, its elements cannot be modified. The same constraint applies to changing the sequence by adding or removing elements. In contrast, lists are designed for situations where items need to be added, removed, or modified frequently.

Tuplefixed elementsTuplefixed elementsElement changenot allowedcoordinatesrelated valuescoordinatessame related valuescontainsattemptcontents remain fixedcontains
What changes when code attempts to replace, add, or remove an element from an existing tuple?
Worked example

Choosing a tuple for fixed data

A program stores a group of related coordinate values that should not be changed accidentally. Should the program use a list or a tuple?

  1. 1
    Identify the required behavior

    The coordinate values are intended to stay fixed after creation.

  2. 2
    Match the structure to the behavior

    A tuple is an immutable sequence, so its elements cannot be changed after creation.

  3. 3
    Contrast the alternative

    A list would be more appropriate if the program needed to add, remove, or modify coordinate elements frequently.

Result

Use a tuple when the coordinate sequence should remain fixed; use a list when the sequence needs flexible changes.

Comparing Tuples by Order

Tuples support equality and ordering comparisons. Python compares tuples element by element from left to right. For equality, two tuples must have the same length and all corresponding elements must be equal. For ordering, Python compares the first elements. If those elements are equal, it moves to the second elements, continuing from left to right until the comparison is decided.

Worked example

Finding the first difference

Compare the tuple with elements 2 and 9 to the tuple with elements 3 and 1.

  1. 1
    Compare the first elements

    The first tuple begins with 2, and the second tuple begins with 3.

  2. 2
    Stop at the deciding position

    Because the first elements are different, the comparison is decided at the first position. The later values do not need to decide the ordering.

  3. 3
    Apply the same rule to equality

    For tuples to be equal, every corresponding element must be equal and the tuples must have the same length.

Result

Tuple comparison proceeds from the first element toward the last, and the first unequal position determines an ordering comparison.

The same sequence supports other common operations as well. Tuples can be concatenated, repeated, checked for membership, and sliced. These operations work with the tuple sequence, while the immutability rule still prevents changing the existing tuple's elements.

Tuples as Dictionary Keys

A tuple's immutability makes it hashable. Because tuples are hashable, they can be used as dictionary keys. This allows a fixed sequence of values to identify a dictionary value. Lists do not have this capability among the sequence types discussed here.

Tuple keyimmutable sequenceDictionarykey-value mappingDictionary valueassociated dataused as keymaps to
How does an immutable tuple become a key that maps to a value in a dictionary?
Worked example

Selecting a key structure

A program needs to associate one value with a fixed sequence of related values. Which sequence type is suitable as the dictionary key?

  1. 1
    Check whether the sequence is fixed

    The sequence is intended to remain unchanged after it is created.

  2. 2
    Check the key requirement

    A tuple is immutable and therefore hashable.

  3. 3
    Choose the sequence

    The tuple can serve as the dictionary key, whereas a list cannot serve this role among the sequence types being compared.

Result

Use a tuple when a fixed sequence must act as a dictionary key.

Mistakes to Avoid

  • Common mistake

    Treating a tuple like a list that can be edited in place.

    What it looks like

    Attempting to replace, add, or remove an element from an existing tuple.

    Why it fails

    Tuples are immutable, so their elements cannot be changed after creation.

    Do this instead

    Choose a list when the sequence must change frequently. Choose a tuple when the contents should remain fixed.

  • Common mistake

    Forgetting that tuple access is position-based.

    What it looks like

    Looking for a value without identifying its integer index.

    Why it fails

    Tuple elements are accessed using integer positions and the bracket operator.

    Do this instead

    Count from index 0 for the first element, and use index -1 for the last element.

  • Common mistake

    Comparing only the final elements of two tuples.

    What it looks like

    Ignoring an earlier difference because the last values appear similar.

    Why it fails

    Tuple ordering comparisons proceed from left to right, and the first unequal position decides the ordering.

    Do this instead

    Compare corresponding elements beginning with the first position.

  • Common mistake

    Using a list when a fixed sequence must be a dictionary key.

    What it looks like

    Trying to use a list as the key for a dictionary mapping.

    Why it fails

    Tuples are hashable because they are immutable; lists do not have this capability among the sequence types discussed here.

    Do this instead

    Use a tuple when the sequence should act as a dictionary key.

Practice Check

EASY

A fixed sequence contains the values 10, 20, and 30 in that order. Identify the value at index 0 and the value at index -1. Then decide whether a tuple or a list is the better structure if the values must not be changed. Finally, explain why the tuple could be used as a dictionary key.

Hints
  • Index 0 refers to the first element.
  • Index -1 refers to the last element.
  • Use immutability to decide between a tuple and a list.
  • Connect immutability to hashability.

What do you think happens?

Two tuples are compared. Their first elements are different, but their later elements are equal. Which position determines the ordering comparison?

  • The first position

  • The last position

  • The position with the greatest integer index

Reveal answer
Answer:

The first position

Tuple ordering comparisons proceed from left to right. The first unequal pair of corresponding elements determines the ordering.

Key Takeaways

  1. 1
    Tuples are ordered sequences indexed by integers.
  2. 2
    Index 0 selects the first element, and index -1 selects the last element.
  3. 3
    Tuples are immutable, so their elements cannot be changed after creation.
  4. 4
    Tuple comparisons proceed element by element from left to right.
  5. 5
    Immutability makes tuples hashable and suitable as dictionary keys, while lists are better when sequence contents must change.

At a glance

  • A tuple is an immutable, ordered sequence whose elements are accessed by integer positions.
  • Use index 0 for the first element and index -1 for the last element.
  • Unlike lists, tuples cannot have their elements added, removed, or modified after creation.
  • Python compares tuples from left to right, using the first unequal position to determine ordering.
  • Tuples are hashable and can be used as dictionary keys when a fixed sequence must identify a value.

More concepts starting with C