Dictionaries

Dictionaries

A dictionary is an ordered set of key-value pairs. It provides a way to map pieces of data to each other and allows for quick access to values associated with keys. Keys must be unique in dictionaries, but the values don’t have to be.

Python dictionaries are dynamic and mutable, which means they can change.

If you assign a value to an already existing dictionary key, it does not add the key a second time. Instead, it replaces the existing value. Dictionaries also have several built-in methods which make it easier to manipulate the data structure and objects contained within.

Note: As of Python version 3.7, dictionaries are ordered based on insertion, but this is not the case in Python 3.6 and earlier.

Syntax

The syntax of a dictionary is as follows:

dictionary_name = { key1: value1,  key2: value2,  key3: value3 }

Each entry in a dictionary is a key-value pair. Each pair is separated by a comma.

Dictionary keys must be immutable types such as numbers and strings because keys should not change. Keys cannot be lists because lists are mutable, and it will raise a TypeError.

Values can be any type, such as strings, numbers, lists, and even other dictionaries.

Creating a Dictionary

An empty dictionary is created with curly braces:

Loading...

An empty dictionary can also be created using the built-in function, dict(), with no arguments:

Loading...

A dictionary with entries:

Loading...

The three key-value pairs in the coffee_shop dictionary:

  • "cold brew": 3.50
  • "latte": 4.25
  • "cappucino": 3.99

Accessing a Dictionary

The values in a dictionary can be accessed by passing the associated key name:

Loading...

When you try to access a key that does not exist, it will raise a KeyError.

Iterating Through a Dictionary

There are several ways to iterate through a dictionary depending on which data you want to access: keys, values, or both.

The following codebyte consists of four for loops that iterate through the coffee_shop dictionary:

The for loops access and print each key, value, key-value tuple, and individual key-values in coffee_shop, respectively.

Adding an Entry

To add an entry, use square brackets to create an index into a new_key and assign it a new_value:

Loading...

Creating a Dictionary using Dictionary Comprehension

Like a list comprehension, a dictionary comprehension is a Pythonic way to create a dictionary. They can be used to filter and manipulate data in tons of useful ways. The syntax is as follows:

Loading...

The if condition at the end is optional, but is a great tool for filtering data. For example, given a dictionary with a person’s name and age, make a new dictionary that only contains people with an age under 25:

Loading...

The expression is name: age, as that’s how the new dictionary will be formatted. Then, the typical for loop iteration, for name, age in person_age.items(). Lastly, the if condition filters out the results.

The expression can also perform operations on the data being extracting. For example, to create a dictionary with key value pairs of a number and its square given a list of numbers:

Loading...

nums_squared will produce a result of: { 1: 1, 2: 4, 3: 9, 4: 16, 5: 25 }

Interested in helping build Docs? Read the Contribution Guide or share your feedback.

Learn Python on Codecademy