Concepts / Collections: Lists, Tuples, and Dictionaries

Collections: Lists, Tuples, and Dictionaries

A tuple 1 is a sequence of values much like a list. The values stored in a tuple can be any type, and they are indexed by integers. The important difference is that tuples are immutable . Tuples are also comparable and hashable so we can sort lists of them and use tuples as key values in Python dictionaries.

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

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.

identifiesidentifiesidentifies0positionredtuple value1positiongreentuple value2positionbluetuple value
How does each integer index map to the corresponding value in a tuple?

Reading a Tuple by Position

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

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

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

Read the value: The value connected to that position is green.

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.

element modifiedsame modification attemptedListred, greenTuplered, greenListred, blueTuplered, green
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

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.

maps tored, greentuple keycolor pairdictionary value
How does a tuple become a dictionary key, and how does that key connect to its stored value?

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.

Tracing a Dictionary Item

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

Start with the dictionary: A dictionary organizes related values as key-value pairs.

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

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

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

Common Selection Mistakes

  • Treating a tuple as if it were a mutable list.

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

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

  • Forgetting that tuple values are selected through integer indexes.

    Tuple values are indexed by integers.

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

  • Assuming that tuples are only useful as ordinary sequences.

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

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

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

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

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

Key Takeaways

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