Working with Lists and Dictionaries
If you have used keyword arguments in your functions, you have already used dictionaries! Just think about it - the key-value pair is specified by you in the parameter list of the function definition and when you access variables within your function, it is just a key access of a dictionary (which is called the symbol table in compiler design terminology).
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?
Identify the key: The name label is the key in the key-value pair.
Identify the value: The supplied value active is the value associated with that key.
Interpret 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?
Sequence: The sequence values are identified through integer indexing.
Dictionary: The dictionary values are identified through their keys, such as color or status.
Compare 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?
Start with the dictionary: The dictionary organizes the information as key-value pairs.
Apply items: The items method returns a list of tuples.
Interpret 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
Thinking that dictionaries and sequences identify values in the same way.
The source distinguishes integer indexing for sequences from key-based access for dictionaries.
Fix:
Ask whether the collection is organized as a sequence indexed by integers or as key-value pairs.Missing the dictionary connection in keyword arguments.
The source explains that keyword arguments already express key-value pairs.
Fix:
Treat the argument name as the key and the supplied argument as the associated value.Assuming that items returns another dictionary.
The source states that items returns a list of tuples.
Fix:
Remember that each tuple in the returned list represents one key-value pair.Forgetting that tuples are immutable.
The source identifies immutability as the important difference between tuples and lists.
Fix:
Treat 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.
- The 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.