Concepts / Dictionary Fundamentals: Keys and Values

Dictionary Fundamentals: Keys and Values

A tuple is a sequence of values whose elements cannot be changed after the tuple is created.

  • Programming

A Sequence That Stays Fixed

A tuple is a sequence of values whose elements cannot be changed after the tuple is created. It resembles a list because both sequences use integer positions for access, but a tuple is designed for data that should remain fixed. That fixed structure becomes especially useful when a sequence needs to serve as a dictionary key.

Worked example

A fixed group of related values

Consider the tuple (51.5, -0.1), representing a pair of related values with a fixed structure.

  1. 1
    Create

    The two values are grouped into one tuple sequence.

  2. 2
    Access

    Position 0 refers to 51.5, while position 1 refers to -0.1.

  3. 3
    Protect

    After creation, the tuple's elements cannot be replaced.

Result

The tuple provides an ordered, fixed sequence that can be accessed by integer position.

Mapping Positions to Elements

Tuple positions are integer indices. Index 0 identifies the first element, and counting continues from there. Negative indexing counts from the end: index -1 identifies the last element. The bracket operator is used to access the element at a chosen index.

0first positionnorthtuple element1second positioneasttuple element-1last positionwesttuple elementselectsselectsselects
How does each integer index map to a specific element in a tuple?

In the visual, the tuple is treated as the ordered sequence (north, east, west). The positive indices identify positions from the beginning, while -1 identifies the final element from the end. Indexing retrieves an element; it does not alter the tuple.

Why Assignment Stops Working

Immutability means that once a tuple exists, its elements cannot be replaced, added, or removed. A list allows its contents to be modified after creation, but a tuple locks its contents in place. Therefore, an attempted change to an existing tuple element does not produce a modified tuple.

(red, green)existing tuple(red, green)tuple after attemptgreenelement at index 1greenelement at index 1replace greenmodification attemptcontainscontainsdoes not modify
What changes when an existing tuple element is targeted for modification, and what remains unchanged?

From Tuple to Dictionary Key

Tuples are hashable because they are immutable. This allows a tuple to be used as a dictionary key, whereas lists do not have this capability. The tuple acts as the key, and the dictionary associates that key with a value. A fixed sequence can therefore identify one dictionary entry.

(52, 8)immutable tuple keyoccupiedassociated dictionary valuemaps to
Why can an immutable tuple serve as a dictionary key, and how does that key connect to its associated value?
Worked example

Using a fixed pair as an identifier

Imagine a dictionary in which the tuple (52, 8) identifies one entry.

  1. 1
    Form the key

    The two integers are grouped into an immutable tuple.

  2. 2
    Use the key

    The tuple can identify a dictionary entry because tuples are hashable.

  3. 3
    Associate a value

    The dictionary connects that tuple key with its associated value.

Result

The tuple provides a fixed, hashable sequence that can function as a dictionary key.

The key idea is the connection between immutability and hashability. Because the tuple cannot change after creation, it is suitable for use as a stable dictionary key. A list is mutable, so it does not provide the same key capability.

Choosing Tuples or Lists

QuestionTupleList
Can elements be changed after creation?No; the elements are immutable.Yes; list contents can be modified.
Can it be used as a dictionary key?Yes, because tuples are hashable.No, because lists do not have this capability.
When is it appropriate?When the sequence should remain fixed.When items need to be added, removed, or modified frequently.

Choose according to whether the sequence should be fixed or flexible.

Tuples are useful for fixed structures such as coordinates, RGB color values, grouped related data, and values returned together from a function. Lists are more suitable when the sequence must change frequently. The choice is based on whether the contents should be fixed or flexible.

Key point

Tuple immutability does not prevent useful sequence operations. Tuples support concatenation, repetition, membership checks, slicing, and element-by-element comparison. These operations produce or inspect sequence information without changing the existing tuple.

Mistakes with Fixed Sequences

  • Common mistake

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

    What it looks like

    Trying to replace one existing tuple element after the tuple has been created.

    Why it fails

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

    Do this instead

    Create a new tuple when a different fixed sequence is needed, or choose a list when the contents must change.

  • Common mistake

    Forgetting that indexing begins at 0.

    What it looks like

    Assuming index 1 identifies the first element.

    Why it fails

    The first tuple element is at index 0.

    Do this instead

    Use 0 for the first element and -1 for the last element.

  • Common mistake

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

    What it looks like

    Trying to use a mutable list as the sequence-based key for a dictionary entry.

    Why it fails

    Lists do not have the hashable capability that allows tuples to serve as dictionary keys.

    Do this instead

    Use a tuple when the sequence is fixed and needs to function as a dictionary key.

  • Common mistake

    Assuming that being immutable means a tuple cannot be inspected or combined.

    What it looks like

    Avoiding slicing, membership checks, concatenation, or comparisons because the tuple cannot be edited.

    Why it fails

    Many sequence operations work on tuples; immutability means the existing tuple is not changed.

    Do this instead

    Use supported sequence operations to inspect or produce sequence results while keeping the original tuple fixed.

Trace the Tuple

EASY

Consider the tuple (blue, yellow, black). Identify the element selected by index 0, the element selected by index 1, and the element selected by index -1. Then decide whether replacing the element at index 1 would change the existing tuple. Finally, explain why this tuple could serve as a dictionary key.

Hints
  • Start counting positive indices from 0.
  • Use -1 for the final element.
  • Connect immutability to hashability.

A complete reasoning path is: index 0 selects blue, index 1 selects yellow, and index -1 selects black. Replacing the element at index 1 cannot modify the existing tuple because tuple elements are immutable. The tuple can serve as a dictionary key because its immutability makes it hashable.

Essential Takeaways

  1. 1
    A tuple is an immutable sequence whose elements are accessed with integer indices.
  2. 2
    Index 0 selects the first element, and index -1 selects the last element.
  3. 3
    An existing tuple cannot have its elements replaced, added, or removed.
  4. 4
    Tuple immutability makes tuples hashable, so tuples can be used as dictionary keys while lists cannot.
  5. 5
    Choose tuples for fixed data and lists for data that needs frequent modification.

At a glance

  • Tuples are fixed, immutable sequences indexed by integers.
  • Use 0 for the first tuple element and -1 for the last.
  • Immutability prevents changes to existing tuple elements.
  • Because tuples are hashable, they can be used as dictionary keys.
  • Use a tuple for fixed data and a list for flexible data.

Related concepts

More concepts starting with D