Reference guide: Dictionaries


There are two main ways to create dictionaries in Python:

  • Braces: {}
  • The dict function: dict()

When instantiating a dictionary using braces, separate each element with a colon. For example, the following code creates a dictionary containing continents as keys and their smallest countries as values:

smallest_countries = {'Africa': 'Seychelles',
                     'Asia': 'Maldives',
                     'Europe': 'Vatican City',
                     'Oceania': 'Nauru',
                     'North America': 'St. Kitts and Nevis',
                     'South America': 'Suriname'
                     }

To create an empty dictionary, use empty braces or the dict() function:

empty_dict_2 = dict()

The dict() function uses a different syntax, where keys are entered as the function’s keyword arguments and values are assigned with an equals operator:

smallest_countries = dict(africa='Seychelles',
                         asia='Maldives',
                         europe='Vatican City',
                         oceania='Nauru',
                         north_america='St. Kitts and Nevis',
                         south_america ='Suriname'
)

Work with dictionaries
To access a specific value in a dictionary, you must refer to its key using brackets:

my_dict = {'nums': [1, 2, 3],
          'abc': ['a', 'b', 'c']
          }
print(my_dict['nums'])

To access all values in a dictionary, use the values() method:

my_dict = {'nums': [1, 2, 3],
          'abc': ['a', 'b', 'c']
          }
print(my_dict.values())

Dictionaries are mutable data structures in Python. You can add to and modify existing dictionaries. To add a new key to a dictionary, use brackets:

my_dict = {'nums': [1, 2, 3],
          'abc': ['a', 'b', 'c']
          }


# Add a new 'floats' key
my_dict['floats'] = [1.0, 2.0, 3.0]
print(my_dict)

Check if a key exists in a dictionary:

smallest_countries = {'Africa': 'Seychelles',
                     'Asia': 'Maldives',
                     'Europe': 'Vatican City',
                     'Oceania': 'Nauru',
                     'North America': 'St. Kitts and Nevis',
                     'South America': 'Suriname'
                     }


print('Africa' in smallest_countries)
print('Asia' not in smallest_countries)

To delete a key-value pair from a dictionary, use the del keyword:

my_dict = {'nums': [1, 2, 3],
          'abc': ['a', 'b', 'c']
          }
del my_dict['abc']
print(my_dict)

Dictionaries are a core Python class. As you’ve learned, classes package data with tools to work with it. Methods are functions that belong to a class. Dictionaries have a number of built-in methods that are very useful. Some of the most commonly used methods include:

items()

Return a view of the (key, value) pairs of the dictionary:

my_dict = {'nums': [1, 2, 3],
          'abc': ['a', 'b', 'c']
          }
print(my_dict.items())

keys() 

Return a view of the dictionary’s keys:

my_dict = {'nums': [1, 2, 3],
          'abc': ['a', 'b', 'c']
          }
print(my_dict.keys())

values() 

Return a view of the dictionary’s values:

my_dict = {'nums': [1, 2, 3],
          'abc': ['a', 'b', 'c']
          }
print(my_dict.values())

Note that the objects returned by these methods are view objects. They provide a dynamic view of the dictionary’s entries, which means that, when the dictionary changes, the view reflects these changes. Dictionary views can be iterated over to yield their respective data. They also support membership tests.