Please enable JavaScript to use CodeHS

Dictionaries in Python

Learn how to use dictionaries to store data in your Python programs.

By Rachel Devaney

A dictionary is an unordered data structure that stores key-value pairs. Dictionaries enable us to access values with their associated key, as opposed to lists, which use indices to access values. Dictionaries are created with curly brackets:

my_dictionary = {}
Plain text

You can add initial key-value pairs to a dictionary like this:

my_dictionary = {
    "Tracy": "314-1592",
    "Karel": "123-4567"
}
Plain text

A few things to note about this structure:

  • The key is on the left and its associated value is on the right.
  • The key and value are separated by a colon :
  • Multiple key-value pairs are separated by a comma

Key and Value Data Types

The keys and values in a single dictionary can be multiple data types. For example, we can add the key-value pair 225: 15 to this dictionary even though the current entries are strings.

my_dictionary = {
    "Tracy": "314-1592",
    "Karel": "123-4567",
    225: 15
}
Plain text

Additionally, the key and value data types can be different themselves: 225: "perfect square" is a valid key-value pair. One important note is that a key cannot be a mutable data type, such as a list or a set, while a value can be a mutable data type.

Accessing Values in a Dictionary

To access a value in a dictionary, we use its key. The general structure for accessing a value in a dictionary is like this: dictionary_name[key].

print([my_dictionary["Karel"]) # prints 123-4567
print([my_dictionary[225]) # prints 15
Plain text

You can use a for loop to iterate through a dictionary and access each individual value by using the in keyword:

for key in my_dictionary:
    print(my_dictionary[key])
Plain text

This for loop says “for every key in my_dictionary, print out the key’s value.” Here’s the output of the above for loop:

314-1592
123-4567
15
Plain text

Inserting a Key-Value Pair

You can add items to a dictionary using this basic structure: dictionary_name[key] = value. Let’s add another key-value pair to my_dictionary:

my_dictionary[256] = 16
Plain text

Now, 256: 16 is a key-value pair in my_dictionary.

Updating a Value

You can update the value of a key in a dictionary by accessing the key and assigning it a new value:

my_dictionary["Karel"] = "653-5897" # assigns the value "653-5897" to "Karel"
print(my_dictionary["Karel"]) # prints 653-5897 instead of 123-4567
Plain text

Example

Take a look at the example dictionary in the code editor below. Explore with these guiding tasks:

  • Insert a new key-value pair
  • Assign one of the keys a different value
  • Add a print statement to the for loop that prints out the key in addition to its associated value

Your Turn

Try creating your own dictionary in the editor below. Your dictionary can be a phone book, a collection of square roots, a weekly dinner menu…the possibilities are endless! Master the basics of dictionaries in Python by completing each task below:

  • Create a dictionary with 2-4 initial values.
  • Insert 2 more key-value pairs.
  • Change the value of one of the keys.
  • Use a for loop to print out each key-value pair.