Concepts / Working with Lists and Dictionaries

Working with Lists and Dictionaries

A tuple is a sequence of values much like a list.

  • Programming

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.

Function callkeyword argumentsKey-value pairname and supplied valueFunctionaccesses the value by nameprovidesavailable to
How do named arguments passed to a function become key-value entries that the function can access?

Tracing a Function's Symbol Table

Worked example

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?

  1. 1
    Identify the key

    The name label is the key in the key-value pair.

  2. 2
    Identify the value

    The supplied value active is the value associated with that key.

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

Result

The function's named value can be understood as a dictionary-style key lookup in its symbol table.

Symbol tabledictionary-style maplabelactiveVariable accesslabelcontainslooks up
What does the function's symbol table contain, and how does accessing a variable correspond to looking up a dictionary key?

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.

Sequenceinteger indexDictionarykey-value pairInteger indexidentifies a valueKeyidentifies a valueusesuses
What is the difference between identifying a sequence value by integer position and identifying a dictionary value by key?
Worked example

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?

  1. 1
    Sequence

    The sequence values are identified through integer indexing.

  2. 2
    Dictionary

    The dictionary values are identified through their keys, such as color or status.

  3. 3
    Compare the models

    The sequence model asks which integer index identifies the value. The dictionary model asks which key identifies the value.

Result

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.

Worked example

Reading the Result of items

A dictionary contains the pairs color: green and status: active. What kind of result does its items method provide?

  1. 1
    Start with the dictionary

    The dictionary organizes the information as key-value pairs.

  2. 2
    Apply items

    The items method returns a list of tuples.

  3. 3
    Interpret each tuple

    Each tuple represents one key-value pair from the dictionary.

Result

The result is a list whose entries are tuples representing the dictionary's key-value pairs.

Dictionarykey-value pairsitemsmethodList of tuplesone tuple per key-valuepaircallsreturns
How does a dictionary's items method represent its key-value pairs?

Common Misunderstandings

  • Common mistake

    Thinking that dictionaries and sequences identify values in the same way.

    What it looks like

    Treating a dictionary lookup as though it were an integer-index lookup.

    Why it fails

    The source distinguishes integer indexing for sequences from key-based access for dictionaries.

    Do this instead

    Ask 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 like

    Viewing a named argument only as a parameter detail.

    Why it fails

    The source explains that keyword arguments already express key-value pairs.

    Do this instead

    Treat 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 like

    Expecting items to preserve the result as a dictionary.

    Why it fails

    The source states that items returns a list of tuples.

    Do this instead

    Remember that each tuple in the returned list represents one key-value pair.

  • Common mistake

    Forgetting that tuples are immutable.

    What it looks like

    Expecting a tuple to be changed after it has been created.

    Why it fails

    The source identifies immutability as the important difference between tuples and lists.

    Do this instead

    Treat a tuple as a sequence whose stored values cannot be changed.

Practice the Mental Model

MEDIUM

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

Related concepts

More concepts starting with W