Python Beginner Concepts Tutorial

Dictionaries Basics

Dictionary Definition

A dictionary is a Python data structure. It contains a collection of indices called keys and a collection of values associated with each key.

In other programming languages, you may hear of a similar data structure to a Python dictionary called a hashtable. The hashtable algorithm is used in the Python source code to implement a dictionary object.

The association of a key and a value is called a key-value pair or an item.

A simple dictionary example is a mapping of English words as keys to Spanish words as values.

In a dictionary, each key is unique.

In [21]:
english_to_spanish = {'one': 'uno'}

In English, one is equalivalent to uno in Spanish.

We enclose a dictionary in curly braces.

one is a key and uno is its value.

Add a key-value pair to a dictionary

The code below adds a key of two with a corresponding value of dos.

In [10]:
english_to_spanish['two'] = 'dose'
In [11]:
{'one': 'uno', 'two': 'dose'}

Modify an existing value in a dictionary

Above, we spelled the Spanish equivalent of the number two incorrectly. We wrote dose when it is spelled dos.

Dictionaries are mutable so we can change this key's value in our structure.

In [12]:
english_to_spanish['two'] = 'dos'
In [13]:
{'one': 'uno', 'two': 'dos'}

Lookup a key's corresponding value

Dictionaries allow for easy lookup of values.

In [14]:

Check if a key appears in a dictionary

The in operator can be used to see if a key exists in a dictionary. If it does, we return True, otherwise False.

In [15]:
'two' in english_to_spanish

Counts for each unique item

A common use case of dictionary is to count the occurences of unique elements in a list or similar data structure. The advantage of using a dictionary here is that we don't have to know ahead of time which letters appear in our list. Also, with a dictionary, we only make keys for letters that do appear.

Below is a list of grades. We want to know the count of each of the grades.

Since each grade letter is unique, each can be a key in a dictionary with its value being the count of occurences.

In [16]:
letter_grades = ["B+", "A", "B+", "A", "A+", "A-", "A"]
In [17]:
count_of_each_grade = {}

for letter in letter_grades:
    if letter in count_of_each_grade:
        count_of_each_grade[letter] += 1
        count_of_each_grade[letter] = 1
In [18]:
{'A': 3, 'A+': 1, 'A-': 1, 'B+': 2}

The dictionary count_of_each_grade tells us there were 3 A grades, 1 A+ and so forth.

Data types allowed for keys and values

In a Python dictionary, the keys must be immutable meaning they can't be changed in place. Therefore, keys can be a data type such as a:

  • tuple (must contain only mutable objects)
  • int
  • float
  • string

A key cannot be a list since lists are mutable and can be modified in place with index assignments or using methods like append or extend.

Values in a dictionary do not have to be unique. The values of a dictionary can be of any data type.

Here's an example of an acceptable dictionary with various data types as keys and values.

In [19]:
a = {(3,2): 1, 9.5: 3.14, 3: (3.1), 'hi': 'yo'}

A more complex example

A common use case of dictionaries in software applications is to store personally identifiable information of users.

In a software application, there could be two users with the same first and last name. Therefore, each user is tyipcally assigned a unique id as an integer.

If we lookup a user's id, we may find information about them as seen below.

In [20]:
users = {10: {'first_name': 'Dan', 'last_name': 'Friedman', 'email': ''},
        11: {'first_name': 'May', 'last_name': 'Parker', 'email': ''}}

This example shows how a value can also be another dictionary of key-value pairs.