Concepts / Iterating Over Dictionaries

Iterating Over Dictionaries

Dictionaries are created empty using dict() or {} and store key-value pairs.

  • Programming

A Dictionary as Ordered Entries

A dictionary stores information as key-value pairs. Iterating over a dictionary means moving through the entries it contains. In Python 3.7 and later, those entries keep the order in which they were added, so the sequence used during iteration matches the insertion sequence.

nextnextnameAdalanguagePythonlevelbeginner
What order are key-value pairs visited and displayed when a dictionary is iterated in Python 3.7 and later?

Building the Dictionary

You can begin with an empty dictionary in either of two ways: dict() or {}. Both forms create an empty dictionary. After creating it, add a key-value pair with square bracket assignment. The general pattern is dictionary[key] = value. The key identifies the entry, and the value is the information stored for that key.

Adding Three Entries

Create an empty dictionary and add the entries name-Ada, language-Python, and level-beginner.

Start empty: Create the dictionary with dict() or with {}. At this point, it contains no key-value pairs.

Add name: Use square bracket assignment to associate the key name with the value Ada.

Add language: Use square bracket assignment to associate the key language with the value Python.

Add level: Use square bracket assignment to associate the key level with the value beginner.

The dictionary contains three key-value pairs, ordered by the sequence in which they were added: name-Ada, language-Python, and level-beginner.

assign levelprofilename: Adaprofilename: Ada, level: beginner
How does a dictionary change before and after assigning a value to a new key?

Following the Iteration Sequence

Once a dictionary has several entries, iteration visits them in insertion order in Python 3.7 and later. If the entries were added in the order name, language, and level, iteration moves through name first, language second, and level third. The important idea is that the dictionary does not lose the sequence in which its entries were inserted.

first entrynextnextno entries remainStartnameAdalanguagePythonlevelbeginnerEnd
What does the loop visit at each step, and how does control move from one dictionary entry to the next?

What do you think happens?

A dictionary receives the entries first, second, and third in that order. Which entry is visited first during iteration in Python 3.7 and later?

  • first
  • second
  • third
Reveal answer

Answer: first

Python 3.7 and later preserve insertion order, so iteration follows the order in which the entries were added.

Reading Dictionary Output

A dictionary's output shows its key-value pairs in dictionary notation. That output format is also valid input syntax, which means a displayed dictionary with multiple pairs can be used to create a dictionary containing those pairs. The pairs shown in the output correspond to the pairs that were stored in the dictionary, and their displayed sequence follows insertion order in Python 3.7 and later.

storesstoresappears asappears asDictionary inputname: Ada, level: beginnernameAdalevelbeginnerDictionary outputname: Ada, level: beginner
How do the key-value pairs written when creating a dictionary correspond to the pairs shown when it is printed or iterated?

Mistakes to Avoid

  • Confusing an empty dictionary with a dictionary that already contains entries.

    The dictionary must receive entries after it is created.

    Fix: Use square bracket assignment to add each key-value pair.

  • Expecting dictionary entries to appear in an arbitrary order.

    Python 3.7 and later preserve insertion order.

    Fix: Track the order in which entries are added and expect iteration to follow that sequence.

  • Treating dictionary output as unrelated display text.

    The output format of a dictionary is valid input syntax for creating a dictionary with multiple items.

    Fix: Use the displayed key-value structure to understand how the dictionary was represented and how multiple pairs can be supplied together.

Check Your Understanding

EASY

Describe the final dictionary and its iteration order after creating an empty dictionary, adding color-red, adding size-large, and adding status-ready in that sequence. Then explain how the dictionary's output format would represent those three pairs.

Hints
  • List the entries in the order they were added.
  • Remember that Python 3.7 and later preserve insertion order.
  • The output represents the same key-value pairs stored in the dictionary.
  1. A dictionary can start empty with dict() or {}. Add entries with square bracket assignment in the form dictionary[key] = value. In Python 3.7 and later, iteration follows insertion order. The dictionary representation shown as output contains the same key-value pairs and is also valid syntax for creating a dictionary with multiple items.

Key Takeaways

  • dict() and {} both create empty dictionaries.
  • Square bracket assignment adds key-value pairs.
  • Python 3.7 and later preserve the order in which dictionary entries are inserted.
  • Iteration and displayed output follow that insertion sequence.
  • A dictionary's output format is also valid input syntax for creating multiple entries.