Concepts / Collections: Lists, Tuples, and Dictionaries

Collections: Lists, Tuples, and Dictionaries

Lists, tuples, and dictionaries are collection data structures, but they organize and use values in different ways.

  • Programming

Why Collection Choice Matters

A collection brings related values together in a data structure. Lists, tuples, and dictionaries are collection data structures, but they organize and use values in different ways. The most important distinction in this topic is that a tuple is a sequence like a list, while a tuple cannot be changed after it has been created.

Key point

When choosing between a list and a tuple, ask whether the collection needs to be changed. The source identifies tuples as immutable and lists as the contrasting mutable collection.

Following Tuple Indexes

A tuple is a sequence of values. Each value is identified by an integer index, so an index connects a position in the sequence to the value stored there. For example, consider the generated tuple sequence containing the values red, green, and blue. The first integer index identifies red, the next identifies green, and the next identifies blue.

0positionredtuple value1positiongreentuple value2positionbluetuple valueidentifiesidentifiesidentifies
How does each integer index map to the corresponding value in a tuple?
Worked example

Reading a Tuple by Position

A tuple contains the generated sequence red, green, blue. Which value is connected to integer index 1?

  1. 1
    Identify the sequence

    The tuple contains three values in sequence order: red, green, and blue.

  2. 2
    Locate the index

    Integer index 1 is connected to the second position shown in the sequence.

  3. 3
    Read the value

    The value connected to that position is green.

Result

Integer index 1 identifies green in this generated tuple example.

The Immutability Boundary

The defining difference between a tuple and a list is mutability. A tuple is immutable, which means its stored sequence cannot be modified. A list is the mutable counterpart in this comparison, so modifying an element changes the list. The same attempted element modification does not change the tuple.

Listred, greenTuplered, greenListred, blueTuplered, greenelement modifiedsame modification attempted
What changes when an element is modified in a list, and what happens when the same modification is attempted on a tuple?

Comparing Lists and Tuples

PropertyListTuple
Collection roleA collection data structure described as similar to a tupleA sequence of values
Value typesNot specified separately in the source packValues can be any type
IndexingNot specified separately in the source packValues are indexed by integers
MutabilityMutable in contrast with the immutable tupleImmutable
Comparison and hashingNot specified separately in the source packComparable and hashable

The source gives the tuple’s detailed properties and presents the list as the closely related, mutable collection.

Lists and tuples are similar enough to compare, but their mutability leads to different uses. A list is appropriate when the collection needs to change. A tuple is appropriate when the sequence should remain fixed. Tuples also have two additional properties identified in the source: they are comparable and hashable.

Tuples Inside Dictionaries

A tuple can serve as a key value in a Python dictionary because tuples are hashable. The dictionary connects that tuple key to a stored value. This gives a tuple a role beyond being a sequence: the same sequence can identify an associated dictionary value.

red, greentuple keycolor pairdictionary valuemaps to
How does a tuple become a dictionary key, and how does that key connect to its stored value?
Key point

The source links tuple hashability to dictionary use: tuples can be used as key values in dictionaries.

Dictionary Items as Tuples

The relationship also works in the other direction. A dictionary has a method called items that returns a list of tuples. Each returned tuple represents one key-value pair. Therefore, tuples can appear as the individual records produced from dictionary contents.

Worked example

Tracing a Dictionary Item

A dictionary contains a key connected to a value. What kind of collection does its items method return?

  1. 1
    Start with the dictionary

    A dictionary organizes related values as key-value pairs.

  2. 2
    Apply items

    The items method returns the dictionary’s entries as a list.

  3. 3
    Inspect each entry

    Each entry in that returned list is a tuple containing one key-value pair.

Result

The items method returns a list of tuples, with each tuple representing a key-value pair.

Common Selection Mistakes

  • Common mistake

    Treating a tuple as if it were a mutable list.

    What it looks like

    Assuming that an element in a tuple can be replaced while the tuple remains the same collection.

    Why it fails

    The defining difference identified in the source is that tuples are immutable.

    Do this instead

    Use a list when the collection must be modified; use a tuple when its sequence should remain fixed.

  • Common mistake

    Forgetting that tuple values are selected through integer indexes.

    What it looks like

    Trying to identify a tuple value without considering its integer position.

    Why it fails

    Tuple values are indexed by integers.

    Do this instead

    Trace the integer index to the corresponding position in the tuple sequence.

  • Common mistake

    Assuming that tuples are only useful as ordinary sequences.

    What it looks like

    Ignoring the possibility of using a tuple as a dictionary key.

    Why it fails

    Tuples are hashable and can be used as key values in dictionaries.

    Do this instead

    Consider a tuple key when a fixed sequence needs to identify a dictionary value.

  • Common mistake

    Missing the tuple structure returned by a dictionary’s items method.

    What it looks like

    Treating each item as an unrelated value instead of a key-value pair.

    Why it fails

    The items method returns a list of tuples, where each tuple is a key-value pair.

    Do this instead

    Read each returned tuple as one dictionary entry: its key together with its value.

Check Your Understanding

MEDIUM

A collection must hold a sequence of values, allow integer indexing, and remain unchanged after creation. Which collection from this article best fits those requirements? Then explain one additional use of that collection involving dictionaries.

Hints
  • Look for the collection described as an immutable sequence.
  • Recall the connection between tuple hashability and dictionary keys.
EASY

A dictionary’s items method is used. Describe the shape of the result and the meaning of one element in that result.

Hints
  • The result is a list.
  • Each element in that list is a tuple representing a key-value pair.

Summary

  1. 1
    A tuple is a sequence of values indexed by integers.
  2. 2
    Tuple values can be of any type.
  3. 3
    Tuples are immutable, while lists are the mutable collection used for contrast.
  4. 4
    Tuples are comparable and hashable, so lists of tuples can be sorted and tuples can be used as dictionary keys.
  5. 5
    A dictionary’s items method returns a list of tuples, with each tuple representing a key-value pair.

At a glance

  • Tuples are integer-indexed sequences whose values can be any type.
  • The key difference between lists and tuples is mutability: lists can change, while tuples are immutable.
  • Tuples are comparable and hashable.
  • Tuple properties support sorting lists of tuples and using tuples as dictionary keys.
  • A dictionary’s items method returns a list of key-value-pair tuples.

Related concepts

More concepts starting with C