Working with Lists and Dictionaries
A tuple is a sequence of values much like a list.
From Arguments to Entries
You may already have used a dictionary without creating one directly. When keyword arguments are used in a function, each named argument can be viewed as a key-value pair: the argument name is the key and the supplied argument is the value. This gives a practical connection between function calls and dictionaries.
Tracing a Function's Symbol Table
A Named Value Inside a Function
Suppose a function receives the keyword argument label with the value active. How can the function's access to label be understood?
- 1Identify the key
The name label is the key in the key-value pair.
- 2Identify the value
The supplied value active is the value associated with that key.
- 3Interpret the access
When the function accesses label, the access can be understood as looking up the key label in the function's symbol table.
The function's named value can be understood as a dictionary-style key lookup in its symbol table.
The source describes the function's symbol table as a dictionary-like structure in compiler terminology. In this model, the names used inside the function act like keys, and the values associated with those names can be found through key access. This is the central link between working with dictionaries and working with variables inside functions.
Sequences and Keyed Collections
A tuple is a sequence of values much like a list. Its values can be of any type, and the values are indexed by integers. A dictionary is organized around key-value pairs instead. The difference in viewpoint is important: a sequence uses an integer index to identify a value, while a dictionary uses a key to identify its associated value.
Choosing the Identifier
Consider a sequence containing the values red, green, and blue, and a dictionary containing the key-value pairs color: green and status: active. What identifies a value in each collection?
- 1Sequence
The sequence values are identified through integer indexing.
- 2Dictionary
The dictionary values are identified through their keys, such as color or status.
- 3Compare the models
The sequence model asks which integer index identifies the value. The dictionary model asks which key identifies the value.
Use an integer index to identify a sequence value and a key to identify a dictionary value.
Inspecting Key-Value Pairs
Dictionaries provide an items method. The source describes this method as returning a list of tuples, with each tuple representing one key-value pair. This creates a bridge between dictionaries and tuples: a dictionary organizes information by keys, while items presents its pairs as a collection of two-part tuple values.
Reading the Result of items
A dictionary contains the pairs color: green and status: active. What kind of result does its items method provide?
- 1Start with the dictionary
The dictionary organizes the information as key-value pairs.
- 2Apply items
The items method returns a list of tuples.
- 3Interpret each tuple
Each tuple represents one key-value pair from the dictionary.
The result is a list whose entries are tuples representing the dictionary's key-value pairs.
Common Misunderstandings
Common mistake Thinking that dictionaries and sequences identify values in the same way.
What it looks likeTreating a dictionary lookup as though it were an integer-index lookup.
Why it failsThe source distinguishes integer indexing for sequences from key-based access for dictionaries.
Do this insteadAsk whether the collection is organized as a sequence indexed by integers or as key-value pairs.
Common mistake Missing the dictionary connection in keyword arguments.
What it looks likeViewing a named argument only as a parameter detail.
Why it failsThe source explains that keyword arguments already express key-value pairs.
Do this insteadTreat the argument name as the key and the supplied argument as the associated value.
Common mistake Assuming that items returns another dictionary.
What it looks likeExpecting items to preserve the result as a dictionary.
Why it failsThe source states that items returns a list of tuples.
Do this insteadRemember that each tuple in the returned list represents one key-value pair.
Common mistake Forgetting that tuples are immutable.
What it looks likeExpecting a tuple to be changed after it has been created.
Why it failsThe source identifies immutability as the important difference between tuples and lists.
Do this insteadTreat a tuple as a sequence whose stored values cannot be changed.
Practice the Mental Model
A function receives the keyword arguments mode with the value review and level with the value beginner. Explain how these arguments can be represented as dictionary-style entries, how accessing mode can be understood through the function's symbol table, and what kind of result a dictionary's items method would return.
Hints
- Treat each argument name as a key and its supplied value as the associated value.
- The function's symbol table can be understood as a dictionary-style structure for name lookup.
- The items method returns a list of tuples, with each tuple representing one key-value pair.
- 1The key idea is to recognize different ways of organizing values. Sequences such as tuples use integer indexing, while dictionaries organize values as key-value pairs. Keyword arguments already have this key-value shape, and a function's symbol table can be understood as a dictionary-style structure in which variable access corresponds to key access.
Key Takeaways
- Keyword arguments can be understood as key-value pairs.
- A function's symbol table can be understood as a dictionary-style structure where variable access corresponds to key access.
- Sequences such as tuples use integer indexing, while dictionaries use keys to identify values.
- A dictionary's items method returns a list of tuples, with each tuple representing a key-value pair.
- Tuples are sequence values that can contain any type, are indexed by integers, and are immutable.