Learn Python: Dictionaries

Learn all about the Python dictionary structure and how to create and use key-value pairs in your code.

Start[missing "en.views.course_landing_page.learn-python-3.course_illustration" translation]

Key Concepts

Review core concepts you need to learn to master this subject

Python dictionaries

my_dictionary = {1: "L.A. Lakers", 2: "Houston Rockets"}

A python dictionary is an unordered collection of items. It contains data as a set of key: value pairs.

Syntax of the Python dictionary

my_dictionary = {1: "L.A. Lakers", 2: "Houston Rockets"}

The syntax for a Python dictionary begins with the left curly brace ({), ends with the right curly brace (}), and contains zero or more key : value items separated by commas (,). The key is separated from the value by a colon (:).

Dictionary value types

my_dictionary = {1: "L.A. Lakers", 2: "Houston Rockets"}

Python allows the values in a dictionary to be any type - string, integer, a list, another dictionary, boolean, etc.

Keys must always be either strings or numbers, but their values can vary in type.

In the example code block, you can see that the keys are always a string or a number (int or float). The values, on the other hand, are many varied data types.

Accessing and writing data in a Python dictionary

my_dictionary = {1: "L.A. Lakers", 2: "Houston Rockets"}

Values in a Python dictionary can be accessed by placing the key within square brackets next to the dictionary. Values can be written by placing key within square brackets next to the dictionary and using the assignment operator (=). If the key already exists, the old value will be overwritten. Attempting to access a value with a key that does not exist will cause a KeyError.

To illustrate this review card, the second line of the example code block shows the way to access the value using the key "song". The third line of the code block overwrites the value that corresponds to the key "song".

.update() method

my_dictionary = {1: "L.A. Lakers", 2: "Houston Rockets"}

Given two dictionaries that need to be combined, Python makes this easy with the .update() function. Used like this: dict1.update(dict2), the key-value pairs of dict2 will be written into the dict1 dictionary. If there happens to be the same key in both dict1 and dict2, the key-value pair in dict2 will overwrite the original.

Python dictionary .pop() method

my_dictionary = {1: "L.A. Lakers", 2: "Houston Rockets"}

Python dictionaries can remove key-value pairs with the .pop() method. The method takes a key as an argument and removes it from the dictionary. At the same time, it also returns the value that it removes from the dictionary.

Dictionary accession methods

my_dictionary = {1: "L.A. Lakers", 2: "Houston Rockets"}

When trying to look at the information in a Python dictionary, there are multiple methods that access the dictionary and return lists of its contents.

.keys() returns the keys (the first object in the key-value pair), .values() returns the values (the second object in the key-value pair), and .items() returns both the keys and the values as a tuple.

Chevron Left Icon
Creating Dictionaries
Lesson 1 of 3
Chevron Right Icon
  1. 1

    A dictionary is an unordered set of key: value pairs. Suppose we want to store the prices of various items sold at a cafe: - Oatmeal is 3 dollars - Avocado Toast is 6 dollars - Carrot Juice ...

  2. 2

    In the previous exercise we saw a dictionary that maps strings to numbers (i.e., [...] ). However, the keys can be numbers as well. For example, if we were mapping restaurant bill subtotals to the...

  3. 3

    We can have a list or a dictionary as a value of an item in a dictionary, but we cannot use these data types as keys of the dictionary. If we try to, we will get a [...] . For example: [...] w...

  4. 4

    A dictionary doesn't have to contain anything. You can create an empty dictionary: [...] We can create an empty dictionary when we plan to fill it later based on some other input. We will expl...

  5. 5

    To add a single key : value pair to a dictionary, we can use the syntax: [...] For example, if we had our [...] object from the first exercise: [...] and we wanted to add a new item, [...] ...

  6. 6

    If we wanted to add multiple key : value pairs to a dictionary at once, we can use the [...] method. Looking at our [...] object from the first exercise: [...] If we wanted to add 3 new room...

  7. 7

    We know that we can add a key by using syntax like: [...] which will create a key [...] and set the value to [...] . But what if we already have an [...] entry in the [...] dictionary? ...

  8. 8

    Let's say we have two lists that we want to combine into a dictionary, like a list of students and a list of their heights, in inches: [...] Python allows you to create a dictionary using a lis...

  9. 9

    So far we have learned: - How to create a dictionary - How to add elements to a dictionary - How to update elements in a dictionary - How to use a list comprehension to create a dictionary from two...

  1. 1

    Now that we know how to create a dictionary, we can start using already created dictionaries to solve problems. In this lesson, you'll learn how to: - Use a key to get a value from a dictionary - ...

  2. 2

    Once you have a dictionary, you can access the values in it by providing the key. For example, let's imagine we have a dictionary that maps buildings to their heights, in meters: [...] Then we c...

  3. 3

    Let's say we have our dictionary of building heights from the last exercise: [...] What if we wanted to know the height of the Landmark 81 in Ho Chi Minh City? We could try: [...] But [...] ...

  4. 4

    We saw that we can avoid [...] s by checking if a key is in a dictionary first. Another method we could use is a [...] : [...] When we try to access a key that doesn't exist, the program will...

  5. 5

    We saw in the last exercise that we had to add a key:value pair to a dictionary in order to avoid a KeyError. This solution is not sustainable. We can't predict every key a user may call and add al...

  6. 6

    Sometimes we want to get a key and remove it from the dictionary. Imagine we were running a raffle, and we have this dictionary mapping ticket numbers to prizes: [...] When we get a ticket numbe...

  7. 7

    Sometimes we want to operate on all of the keys in a dictionary. For example, if we have a dictionary of students in a math class and their grades: [...] We want to get a roster of the students...

  8. 8

    Dictionaries have a [...] method that returns a [...] object (just like a [...] object but for values!) with all of the values in the dictionary. It can be used in the place of a list for ite...

  9. 9

    You can get both the keys and the values with the [...] method. Like [...] and [...] , it returns a [...] object. Each element of the [...] returned by [...] is a tuple consisting of: [...

  10. 10

    In this lesson, you've learned how to go through dictionaries and access keys and values in different ways. Specifically you have seen how to: - Use a key to get a value from a dictionary - Check ...

  1. 1

    This lesson will help you review Python functions by providing some challenge exercises involving dictionaries. As a refresher, function syntax looks like this: [...] For example, a function t...

  2. 2

    [...]

  3. 3

    [...]

  4. 4

    [...]

  5. 5

    [...]

  6. 6

    [...]

  7. 7

    [...]

  8. 8

    [...]

  9. 9

    [...]

  10. 10

    [...]

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo

Learn Python: Dictionaries

Start[missing "en.views.course_landing_page.learn-python-3.course_illustration" translation]