Collections: Lists, Tuples, and Dictionaries
Lists, tuples, and dictionaries are collection data structures, but they organize and use values in different ways.
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.
Reading a Tuple by Position
A tuple contains the generated sequence red, green, blue. Which value is connected to integer index 1?
- 1Identify the sequence
The tuple contains three values in sequence order: red, green, and blue.
- 2Locate the index
Integer index 1 is connected to the second position shown in the sequence.
- 3Read 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.
Comparing Lists and Tuples
| Property | List | Tuple |
|---|---|---|
| Collection role | A collection data structure described as similar to a tuple | A sequence of values |
| Value types | Not specified separately in the source pack | Values can be any type |
| Indexing | Not specified separately in the source pack | Values are indexed by integers |
| Mutability | Mutable in contrast with the immutable tuple | Immutable |
| Comparison and hashing | Not specified separately in the source pack | Comparable 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.
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?
- 1Start with the dictionary
A dictionary organizes related values as key-value pairs.
- 2Apply items
The items method returns the dictionary’s entries as a list.
- 3Inspect 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
Common mistake Treating a tuple as if it were a mutable list.
What it looks likeAssuming that an element in a tuple can be replaced while the tuple remains the same collection.
Why it failsThe defining difference identified in the source is that tuples are immutable.
Do this insteadUse 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 likeTrying to identify a tuple value without considering its integer position.
Why it failsTuple values are indexed by integers.
Do this insteadTrace 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 likeIgnoring the possibility of using a tuple as a dictionary key.
Why it failsTuples are hashable and can be used as key values in dictionaries.
Do this insteadConsider 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 likeTreating each item as an unrelated value instead of a key-value pair.
Why it failsThe items method returns a list of tuples, where each tuple is a key-value pair.
Do this insteadRead each returned tuple as one dictionary entry: its key together with its value.
Check Your Understanding
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.
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
- 1A tuple is a sequence of values indexed by integers.
- 2Tuple values can be of any type.
- 3Tuples are immutable, while lists are the mutable collection used for contrast.
- 4Tuples are comparable and hashable, so lists of tuples can be sorted and tuples can be used as dictionary keys.
- 5A 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.