Concepts / Dictionary Fundamentals: Keys and Values

Dictionary Fundamentals: Keys and Values

Tuples are immutable sequences indexed by integers, meaning their elements cannot be changed after creation.

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

A fixed group of related values

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

Create: The two values are grouped into one tuple sequence.

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

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

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.

selectsselectsselects0first positionnorthtuple element1second positioneasttuple element-1last positionwesttuple element
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.

containscontainsdoes not modify(red, green)existing tuple(red, green)tuple after attemptgreenelement at index 1greenelement at index 1replace greenmodification attempt
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.

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

Using a fixed pair as an identifier

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

Form the key: The two integers are grouped into an immutable tuple.

Use the key: The tuple can identify a dictionary entry because tuples are hashable.

Associate a value: The dictionary connects that tuple key with its associated value.

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.

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.

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

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

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

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

  • Forgetting that indexing begins at 0.

    The first tuple element is at index 0.

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

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

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

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

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

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

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

Key Takeaways

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