Week 10 Lecture: Dictionaries
1. Introduction to Dictionaries
Up to this point, we have used Lists to store collections of data. Lists are ordered sequences. To find data in a list, you usually need to know its position number (index), like 0, 1, or 2.
However, in real life, we do not always look for things by number.
- When you use a real paper dictionary, you do not look for “Word #500.” You look for the word “Apple.”
- When you look up a friend in your phone, you look for their “Name,” not their memory address.
Python provides a tool for this called a Dictionary.
A Dictionary is a collection of Key-Value pairs.
- The Key: The unique label (like a word in a book). Keys must be simple and unchangeable (like Strings or Integers).
- The Value: The data connected to that key. Values can be anything (Strings, Numbers, Lists, etc.).
Why use Dictionaries?
Dictionaries are very fast. When you know the Key, Python finds the Value immediately. You do not need to search through the whole collection one by one.
Creating and Accessing Dictionaries
To create a dictionary, we use curly braces {}. We separate keys and values with a colon :.
# Creating an empty dictionary
empty_dict = {}
# Creating a dictionary with data
student = {
"name": "Alice", # "name" is the Key. "Alice" is the Value.
"age": 19, # "age" is the Key. 19 is the Value.
"enrolled": True # "enrolled" is the Key. True is the Value.
}
To get data out, we use the key name inside square brackets [] or the .get() method.
# Method 1: Direct Access (using brackets)
print(student["name"]) # Output: Alice
# Method 2: Safe Access (using .get)
print(student.get("age")) # Output: 19
Important Warning: KeyError
If you try to use square brackets [] with a key that does not exist, Python will stop your program with an error. This is called a KeyError.
To avoid this crash, you can use the .get() method. If the key is missing, .get() returns None (or a default value you choose) instead of crashing.
# The Safe Way
# If "gpa" is not in the dictionary, this returns 0.0
gpa = student.get("gpa", 0.0)
Example: A Course Catalog
Here is an example of a system that looks up course names using their codes.
course_catalog = {
"CS101": "Intro to Programming",
"MATH201": "Linear Algebra",
"ENG105": "Academic Writing"
}
search_code = "BIO101"
# Using .get() allows us to handle missing data gracefully
result = course_catalog.get(search_code, "Course not found")
print(f"Result: {result}")
# Output: Result: Course not found
2. Modifying Dictionaries
Dictionaries are mutable. This means we can change them, grow them, or shrink them after we create them.
Adding and Updating
In Python, the syntax to add a new item and update an old item is exactly the same.
- Update: If the key is already in the dictionary, Python replaces the old value with the new one.
- Add: If the key is not in the dictionary, Python creates a new pair.
my_dict = {"a": 1, "b": 2}
# Updating an existing key
my_dict["a"] = 100 # "a" is now 100
# Adding a new key
my_dict["c"] = 3 # "c" is added to the dictionary
Deleting
To remove a key and its value, use the del command.
del my_dict["b"] # Removes "b" completely
Checking for Keys (in)
Before you try to change or access a key, it is often good to check if it exists. We use the in keyword. This checks the Keys, not the values.
inventory = {"apple": 10, "orange": 15}
if "apple" in inventory:
print("We sell apples.")
# Example: Adding to stock
if "apple" in inventory:
# We can only add (+) to the value if the key exists!
inventory["apple"] = inventory["apple"] + 20
3. Iterating Through Dictionaries
Just like lists, we often need to use a for loop to look at every item in a dictionary. Because a dictionary has both Keys and Values, we have three options for looping.
Option 1: Loop over Keys
This is the default behavior.
data = {"x": 10, "y": 20}
for key in data:
print(key)
# Output: x, y
Option 2: Loop over Values
Use this if you only care about the data and not the labels.
for val in data.values():
print(val)
# Output: 10, 20
Option 3: Loop over Both (Recommended)
This is usually the most useful method. We use .items(), which gives us both the Key and the Value at the same time. We use two variables in our loop.
# 'k' gets the key, 'v' gets the value
for k, v in data.items():
print(f"Key is {k}, Value is {v}")
Example: Analyzing Grades
We can use a loop to calculate statistics, like the average score or the highest score.
grades = {"Math": 85, "History": 92, "Physics": 78}
total = 0
for subject, score in grades.items():
total += score # Add up the scores
# Calculate average
# len(grades) counts how many pairs are in the dictionary
average = total / len(grades)
print(f"Average: {average}") # Output: 85.0
4. Pattern: Frequency Analysis (Counting)
One of the most important uses of dictionaries in Computer Science is counting.
Imagine you have a list of votes, or a long text with many words. You want to count how many times each item appears. You cannot create a separate variable for every possible word. Instead, you use a dictionary.
- Key: The item you are counting (e.g., “Alice”).
- Value: The number of times you have seen it (e.g., 4).
The Counting Logic
When you look at an item in your list, you must make a decision:
- Have I seen this before? (Is it in the dictionary?)
- Yes: Add 1 to the current count.
- No: It is new. Add it to the dictionary with a count of 1.
Example: The Vote Counter
votes = ["Alice", "Bob", "Alice", "Charlie", "Bob", "Alice"]
vote_counts = {}
for candidate in votes:
if candidate in vote_counts:
# We have seen this candidate before. Increment the count.
vote_counts[candidate] += 1
else:
# First time seeing this candidate. Start count at 1.
vote_counts[candidate] = 1
print(vote_counts)
# Output: {'Alice': 3, 'Bob': 2, 'Charlie': 1}
This pattern turns a disorganized list into organized data that gives us information.
Using Dictionaries for Scoring (Lookup Tables)
A common use for a dictionary is a Lookup Table. This is when we map a specific character or code to a value, like a score in a game.
Example: The Word Game Scorer
Imagine a game like Scrabble. Every letter has a point value. We want to calculate the total score for a word.
- Common letters (a, e, i, o, u) are worth 1 point.
- Rare letters (q, z) are worth 10 points.
The Strategy:
- Create a dictionary where
Key = LetterandValue = Points. - Loop through every letter in the user’s word.
- Look up the score for that letter and add it to a total.
The Code:
# 1. Create the lookup table
letter_scores = {
'a': 1, 'b': 3, 'c': 3, 'd': 2, 'e': 1, 'f': 4, 'g': 2, 'h': 4,
'i': 1, 'j': 8, 'k': 5, 'l': 1, 'm': 3, 'n': 1, 'o': 1, 'p': 3,
'q': 10, 'r': 1, 's': 1, 't': 1, 'u': 1, 'v': 4, 'w': 4, 'x': 8,
'y': 4, 'z': 10
}
word = "Dictionary"
total_score = 0
# 2. Iterate through the string
for char in word:
# Convert to lowercase so it matches our dictionary keys
clean_char = char.lower()
# 3. Get the score
# We use .get(clean_char, 0).
# If the character is not a letter (like space or !), it returns 0.
points = letter_scores.get(clean_char, 0)
total_score += points
print(f"The score for '{word}' is: {total_score}")
How it works:
The .get() method is very important here. If the user types “Hello!”, the exclamation mark ! is not in our dictionary. .get("!", 0) ensures our program doesn’t crash; it just adds 0 points.
Text Cleaning and Frequency Analysis
Data in the real world is messy. Before we can count words using a dictionary, we usually need to “clean” the text using String methods (from Week 9).
Example: The Text Cleaner
We have a sentence: "Apple, banana! Apple; orange."
If we count immediately, Python thinks "Apple," and "Apple;" are different words because of the punctuation.
The Strategy:
- Clean: Replace punctuation with empty strings (
"") and convert everything to lowercase. - Split: Turn the string into a List of words using
.split(). - Count: Use a dictionary to count the frequency of each word.
The Code:
text = "Apple, banana! Apple; orange. Banana... apple?"
# 1. Clean the text
# We replace punctuation with nothing ("")
clean_text = text.replace(",", "").replace("!", "").replace(";", "").replace(".", "").replace("?", "")
# Convert to lowercase so "Apple" and "apple" are the same
clean_text = clean_text.lower()
# 2. Split into a list of words
word_list = clean_text.split()
# 3. Frequency Count
word_counts = {}
for word in word_list:
if word in word_counts:
# We have seen this word. Add 1.
word_counts[word] += 1
else:
# First time seeing this word. Set to 1.
word_counts[word] = 1
print(word_counts)
Output:
{'apple': 3, 'banana': 2, 'orange': 1}
Working with Lists of Dictionaries
In data science and web programming, data is often structured as a List of Dictionaries.
- The List holds the collection of items (rows).
- Each Dictionary holds the details for one item (columns).
Example: The Sales Report
We have a list of sales. We want to calculate the total revenue for each type of item (e.g., Total money from Laptops, Total money from Mice).
The Data Structure:
sales_log = [
{"item": "Laptop", "price": 1000},
{"item": "Mouse", "price": 20},
{"item": "Laptop", "price": 1000},
]
The Code:
sales_log = [
{"item": "Laptop", "price": 1000},
{"item": "Mouse", "price": 20},
{"item": "Laptop", "price": 1000},
{"item": "Keyboard", "price": 50},
{"item": "Mouse", "price": 25}
]
revenue_report = {}
for sale in sales_log:
# Extract data from the current dictionary inside the list
item_name = sale["item"]
item_price = sale["price"]
# Add to our report dictionary
if item_name in revenue_report:
# Add price to existing total
revenue_report[item_name] += item_price
else:
# Create new entry
revenue_report[item_name] = item_price
print("Total Revenue per Item:")
for item, total in revenue_report.items():
print(f"{item}: ${total}")
How it works:
- We loop through
sales_log. In the first loop,saleis{"item": "Laptop", "price": 1000}. - We pull out “Laptop” and 1000.
- We check our
revenue_reportdictionary. - Eventually,
revenue_reportwill contain{"Laptop": 2000, "Mouse": 45...}. We have successfully summarized the list.
Grouping Data (Inverting a Dictionary)
Sometimes data comes in “backwards” for what we need. Imagine you have a list of students and the team they belong to.
- Current Data: Key is Student, Value is Team.
- Goal: Key is Team, Value is a List of Students.
This is called “inverting” or “grouping” data. The Value in our new dictionary will be a List.
Example: The Team Organizer
The Data Structure:
# What we have:
assignments = {
"Alice": "Red Team",
"Bob": "Blue Team",
"Charlie": "Red Team",
"David": "Green Team",
"Eve": "Blue Team"
}
# What we want:
# {
# "Red Team": ["Alice", "Charlie"],
# "Blue Team": ["Bob", "Eve"],
# "Green Team": ["David"]
# }
The Code:
assignments = {
"Alice": "Red Team",
"Bob": "Blue Team",
"Charlie": "Red Team",
"David": "Green Team",
"Eve": "Blue Team"
}
teams = {}
for student, team_color in assignments.items():
# Check: Does this team exist in our new dictionary yet?
if team_color not in teams:
# No. Create a new key with an empty list.
teams[team_color] = []
# Now we know the list exists. Append the student to it.
teams[team_color].append(student)
print(teams)
How it works:
- First iteration (Alice/Red): “Red Team” is not in
teams. We createteams["Red Team"] = []. Then we append “Alice”. - Second iteration (Bob/Blue): “Blue Team” is not in
teams. We createteams["Blue Team"] = []. Then we append “Bob”. - Third iteration (Charlie/Red): “Red Team” is in
teams. We do not create a new list. We just append “Charlie” to the existing list.
Merging Dictionaries
In real applications, data often comes from multiple sources. You might have inventory data from Warehouse A and Warehouse B, and you need to combine them into a Master Inventory.
- If an item is only in Warehouse A, copy it.
- If an item is only in Warehouse B, copy it.
- If an item is in both, add the numbers together.
Example: Warehouse Consolidation
The Data:
warehouse_a = {"apple": 100, "banana": 50, "orange": 25}
warehouse_b = {"apple": 200, "pear": 30, "banana": 50}
The Code:
warehouse_a = {"apple": 100, "banana": 50, "orange": 25}
warehouse_b = {"apple": 200, "pear": 30, "banana": 50}
total_inventory = {}
# Step 1: Add everything from Warehouse A
for item, count in warehouse_a.items():
total_inventory[item] = count
# Step 2: Add everything from Warehouse B
for item, count in warehouse_b.items():
if item in total_inventory:
# The item exists (it was in Warehouse A). Add to the total.
total_inventory[item] = total_inventory[item] + count
else:
# The item is new (it was not in Warehouse A). Create it.
total_inventory[item] = count
print("Master Inventory:")
for item, count in total_inventory.items():
print(f"{item}: {count}")
Output:
Master Inventory:
apple: 300
banana: 100
orange: 25
pear: 30
How it works:
This logic handles the “overlap” or “collision” of data. We fill the dictionary with the first dataset, then carefully merge the second dataset by checking if item in total_inventory.
This content will be available starting December 02, 2025.