Concepts / Dictionary Methods

Dictionary Methods

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

  • Programming

A Dictionary Before It Has Data

A dictionary begins as a container for key-value pairs. You can create that container empty, then add pairs one at a time. Python provides two forms for creating an empty dictionary: dict() and {}.

python
Output
{}
{}
createscreatesdict()empty dictionary{}no key-value pairs{}empty dictionary
How do dict() and {} produce the same empty dictionary despite using different syntax?

Adding the First Pair

Square bracket assignment adds a key-value pair to a dictionary. The general form is dictionary[key] = value. The expression inside the brackets identifies the key, and the expression after the equals sign supplies the associated value.

profile = {} profile["name"] = "Mira" profile["level"] = 3 print(profile)

assignproducesassignproduces{}empty dictionaryprofile["name"] ="Mira"add one pairname: Mirafirst pairprofile["level"] = 3add another pairname: Mira, level: 3two pairs
How does assigning dictionary[key] = value change the dictionary, and where does the new key-value pair go?

Reading the Dictionary State

A dictionary is organized as key-value pairs. In the displayed form, each key is followed by its associated value, and multiple pairs appear together inside curly brackets. This makes the displayed dictionary easy to read as a representation of the data that was entered.

containsassociated withcontainsassociated withdictionarycollection of pairsnamekeyMiravaluelevelkey3value
What does each key contain, what value is associated with it, and how are multiple pairs organized inside a dictionary?

Building a Dictionary Step by Step

Create a dictionary containing a color and a quantity by starting with an empty dictionary.

Start empty: Use {} to create a dictionary with no key-value pairs.

Add the color: Use inventory["color"] = "blue" to add the key color and its value blue.

Add the quantity: Use inventory["quantity"] = 4 to add the key quantity and its value 4.

Inspect the result: The dictionary display contains both pairs inside curly brackets.

{'color': 'blue', 'quantity': 4}

Insertion Order in Python 3.7 and Later

In Python 3.7 and later, dictionaries preserve insertion order. That means dictionary items appear in the sequence in which you added them. If one pair is added first and another pair is added second, the displayed dictionary keeps that order.

python
Output
{'first': 'open', 'second': 'save', 'third': 'close'}
thenthenfirst: openadded firstsecond: saveadded secondthird: closeadded third
What order are key-value pairs stored and displayed in after they are added one by one?

From Input Syntax to Display

The format Python displays for a dictionary is also valid input syntax for creating a dictionary with multiple items at once. Curly brackets surround the pairs, and each pair is written in key-value form. This gives you two connected ways to understand dictionaries: the displayed form shows the structure, and the same structure can be entered as dictionary input.

same dictionary format{"mode": "quiet","retries": 2}input form{"mode": "quiet","retries": 2}displayed form
How does entering key-value pairs correspond to the dictionary representation Python displays?

Mistakes with Dictionary Construction

  • Using parentheses as though they were the empty-dictionary syntax.

    The two empty-dictionary forms covered here are dict() and {}.

    Fix: Use items = dict() or items = {}.

  • Leaving out the square brackets when adding a pair.

    A dictionary key is specified inside square brackets in the assignment form.

    Fix: Write profile["name"] = "Mira".

  • Expecting dictionary pairs to appear in a different order from the order of insertion in Python 3.7 and later.

    Python 3.7 and later preserve insertion order, so second appears before first in the displayed sequence.

    Fix: Read the displayed order as the order in which the pairs were added.

  • Treating the displayed dictionary format as unrelated to input syntax.

    The dictionary representation shown by Python is also valid input syntax for creating multiple pairs at once.

    Fix: Use the displayed key-value structure as a model for writing a dictionary literal.

Practice the State Changes

EASY

Create an empty dictionary using curly brackets. Add the pair "language": "Python", then add the pair "version": 3.7. Write the dictionary you expect Python to display and preserve in order.

Hints
  • Begin with settings = {}.
  • Use square bracket assignment for each pair.
  • Place the pair added first before the pair added second in the displayed dictionary.

What do you think happens?

What will Python display after these assignments? record = {} record["topic"] = "dictionaries" record["length"] = 1

  • {"topic": "dictionaries", "length": 1}
  • {"length": 1, "topic": "dictionaries"}
Reveal answer

Answer: {"topic": "dictionaries", "length": 1}

In Python 3.7 and later, dictionary items appear in the sequence in which they were added. topic was added before length.

Dictionary Construction Checklist

  1. Create an empty dictionary with dict() or {}.
  2. Add a key-value pair with dictionary[key] = value.
  3. In Python 3.7 and later, dictionary items appear in insertion order.
  4. A dictionary's displayed key-value format is also valid input syntax for creating multiple pairs at once.

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 pairs are inserted.
  • The dictionary format Python displays can also be used as input syntax for multiple key-value pairs.