2.7. Dictionaries#

Did you know that Noah Webster —- the creator of An American Dictionary of the English Language -— learned 26 languages, including Anglo-Saxon and Sanskrit, to research the origins of the English language? Talk about dedication! While Python dictionaries don’t require learning dozens of languages, they do have something in common with Webster’s work: bringing order to complexity.

2.7.1. What are Dictionaries?#

Dictionaries are characterized by their curly braces {}, and you can think of them as data structures for storing data in key-value pairs. Each key in a dictionary acts as a unique identifier, or label, for its corresponding value. Values on the other hand, can be of any data type.

# Creating a dictionary
student_info = {
    "name": "Brandon",
    "age": 20,
}

student_info
{'name': 'Brandon', 'age': 20}

2.7.1.1. Accessing Values#

We can access a specific value at key by using bracket notation [], or by using the .get() method of dictionaries

# Accessing a value
print(student_info["name"])  # Output: Brandon
print(student_info.get("name"))  # Same Output: Brandon
Brandon
Brandon

2.7.1.2. An Analogy#

Remember, dictionaries are composed of Key-Value Pairs: Each element in a dictionary has a key and a value. Put more simply, the key is like a label, and the value is the information it points to:

The key "name" is like a tag that holds the value "Brandon". The key "age" is like a tag that holds the value 20.

This is analagous to a specific word in the English language having a specific meaning, or definition

2.7.1.3. Same Key, Different Value?#

Let’s consider the case where we input the same key twice:

# Creating a dictionary
student_info = {
    "name": "Brandon",
    "name": "Lynn",
    "age": 20,
    "major": "Data Science"
}

# Accessing a value
print(student_info["name"])  # Output: Lynn
Lynn

Keys must be unique, so if the same key is repeated, Python keeps only the last value. In this case, "name" : "Lynn" replaces "name" : "Brandon". When you access student_info["name"], it gives "Lynn" because that’s the final value assigned to "name".

2.7.2. Adding New Pairs to a Dictionary#

Let’s now make a dictionary using some of the most popular DATA courses at UC Berkeley:

# Dictionary mapping course codes to course titles
courses = {
    "DATA C8": "Foundations of Data Science",
    "DATA C100": "Principles and Techniques of Data Science",
    "DATA C101": "Data Engineering",
    "DATA C140": "Probability for Data Science"
}

# Access the title of Data 8
print(f"Course Title for DATA 8: {courses['DATA C8']}")
Course Title for DATA 8: Foundations of Data Science

We notice that the dictionary doesn’t yet include all the DATA courses offered by UC Berkeley. Some key courses are missing, such as DATA C88C, DATA C104, DATA C102, and DATA 144. Let’s add these courses to our dictionary one at a time.

To do this, we simply provide the key (the course code) and its corresponding value (the course title). For example, here’s how we can add DATA C102, which stands for “Data, Inference, and Decisions”:

courses["DATA C102"] = "Data, Inference, and Decisions"

Repeating this for each course, we will end up with the following dictionary!

# Adding new courses to the dictionary
courses["DATA C88C"] = "Computational Structures in Data Science"
courses["DATA C104"] = "Human Contexts and Ethics of Data"
courses["DATA 144"] = "Data Mining"

# Print the updated dictionary
print(courses)
{'DATA C8': 'Foundations of Data Science', 'DATA C100': 'Principles and Techniques of Data Science', 'DATA C101': 'Data Engineering', 'DATA C140': 'Probability for Data Science', 'DATA C102': 'Data, Inference, and Decisions', 'DATA C88C': 'Computational Structures in Data Science', 'DATA C104': 'Human Contexts and Ethics of Data', 'DATA 144': 'Data Mining'}

2.7.3. .keys()#

Suppose we only wanted to see the course codes, and not exactly the specific course titles. To do this, we can use the .keys() method in Python.

Essentially, .keys() allows you to retrieve all the keys from your dictionary. This is useful when you want to see or iterate through all the keys in your dictionary, without worrying about the associated values.

course_keys = courses.keys()
course_keys
dict_keys(['DATA C8', 'DATA C100', 'DATA C101', 'DATA C140', 'DATA C102', 'DATA C88C', 'DATA C104', 'DATA 144'])

If we wanted to convert this sequence of keys into a list, we can do so using the list() function!

# Convert to a list
key_list = list(course_keys)

print(key_list)
['DATA C8', 'DATA C100', 'DATA C101', 'DATA C140', 'DATA C102', 'DATA C88C', 'DATA C104', 'DATA 144']

2.7.4. .values()#

To get the course titles, we can make use of the .values() method! This is similar to .keys(), but instead grabs the value at each key-value pair.

course_values = courses.values()
course_values
dict_values(['Foundations of Data Science', 'Principles and Techniques of Data Science', 'Data Engineering', 'Probability for Data Science', 'Data, Inference, and Decisions', 'Computational Structures in Data Science', 'Human Contexts and Ethics of Data', 'Data Mining'])

2.7.5. .items()#

Suppose we wanted to look at each key-value pair in this dictionary. To do this, we can use the .items() method in Python. As a note, this will return each pair as a tuple.

course_items = courses.items()
course_items
dict_items([('DATA C8', 'Foundations of Data Science'), ('DATA C100', 'Principles and Techniques of Data Science'), ('DATA C101', 'Data Engineering'), ('DATA C140', 'Probability for Data Science'), ('DATA C102', 'Data, Inference, and Decisions'), ('DATA C88C', 'Computational Structures in Data Science'), ('DATA C104', 'Human Contexts and Ethics of Data'), ('DATA 144', 'Data Mining')])

2.7.6. Replacing Values in a Dictionary#

Oops! It seems we made a mistake in the course title for DATA 144. We listed it as “Data Mining” when in reality, it should be “Data Mining and Analytics.” No worries—Python dictionaries make it easy to fix errors like this. We can update the value of this specific key by simply reassigning it with the correct value.

print("The old title: " + courses["DATA 144"])
The old title: Data Mining
# Correct the course title for Data 144
courses["DATA 144"] = "Data Mining and Analytics" #Reassignment

print("The new title: " + courses["DATA 144"])
The new title: Data Mining and Analytics

2.7.7. Deleting Values using del#

Following historical trends, DATA 144 is not usually offered in Spring semesters. How could we go about deleting it from our dictionary in this case? Well, we can make use of del!

del courses["DATA 144"] #Delete the pair of ("DATA 144", "Data Mining and Analytics")

print(list(courses.items()))
[('DATA C8', 'Foundations of Data Science'), ('DATA C100', 'Principles and Techniques of Data Science'), ('DATA C101', 'Data Engineering'), ('DATA C140', 'Probability for Data Science'), ('DATA C102', 'Data, Inference, and Decisions'), ('DATA C88C', 'Computational Structures in Data Science'), ('DATA C104', 'Human Contexts and Ethics of Data')]

2.7.8. Other Values#

Up to this point, we’ve only used strings as the data type for dictionary values. However, dictionaries aren’t limited to just strings! They can store numbers, lists, and much more. The best part? You can still use all the functions and methods we introduced earlier in this chapter with these different dictionaries. Take a look at the examples below for some inspiration!

2.7.8.1. Using Integers for Enrollment#

# Dictionary with course codes as keys and enrollment numbers as values
course_enrollment = {
    "DATA C8": 1200,
    "DATA C100": 800,
    "DATA C101": 450,
    "DATA C140": 500,
    "DATA 144": 300
}


print(str(course_enrollment['DATA C140']) + " students enrolled in DATA C140")
500 students enrolled in DATA C140

2.7.8.2. Using Lists for Course Instructors#

# Dictionary mapping course codes to a list of instructors
instructors = {
    "DATA C8": ["John DeNero", "Swupnil Sahai"],
    "DATA C100": ["Narges Narouzi", "Joseph E. Gonzalez"],
    "DATA C101": ["Lisa Yan", "Michael Ball"],
    "DATA C140": ["Ani Adhikari"]
}

# Access instructors for Data C100
print(f"Instructors for DATA C100: " + str(instructors['DATA C100']))
Instructors for DATA C100: ['Narges Narouzi', 'Joseph E. Gonzalez']

2.7.8.3. Using Dictionaries for Course Details#

Yup! You can use dictionaries as values inside a dictionary

# Dictionary mapping course codes to detailed course information
course_details = {
    "DATA C8": {
        "title": "Foundations of Data Science",
        "instructors": ["John DeNero", "Swupnil Sahai"],
        "units": 4
    },
    "DATA C100": {
        "title": "Principles and Techniques of Data Science",
        "instructors": ["Narges Narouzi", "Joseph E. Gonzalez"],
        "units": 4
    },
    "DATA 144": {
        "title": "Data Engineering",
        "instructors": ["Zachary Pardos"],
        "units": 3
    }
}

# Access detailed information for Data 140
data_144_info = course_details["DATA 144"]
print("The value for the DATA 144 key: " + str(data_144_info))
The value for the DATA 144 key: {'title': 'Data Engineering', 'instructors': ['Zachary Pardos'], 'units': 3}

2.7.9. Python 🤝 Webster#

Although Python dictionaries differ from traditional ones like Webster’s, they share a similar purpose: organizing information for quick and easy lookup. While a traditional dictionary pairs words with definitions, Python dictionaries pair keys (labels) with values (the associated data). You’ll encounter dictionaries again in the Pandas section of this textbook!