Lesson 33/48 ยท ๐Ÿ“ฆ Data Structures
๐Ÿ“ฆ Data StructuresLesson 33/48
Phase 5 ยท Data Structures25 min

Dictionaries, Key-Value Pairs

Store data that needs to be looked up by name, not position

A dictionary stores key-value pairs, like a real dictionary where words are keys and definitions are values. You look things up by *key*, not by position.

How would you approach this? Think first, then expand.

Creating and using dictionariespython
person = {
    "name": "Alice",
    "age": 30,
    "city": "London"
}

print(person["name"])    # Alice
print(person.get("age")) # 30
person["email"] = "alice@example.com"  # Add new key
del person["city"]       # Remove a key
๐Ÿ”Code TracerStep 1 / 7
trace.py
1person = {"name": "Alice", "age": 30}โ†
2person["city"] = "London"
3person["age"] = 31
4print(person)
Variables
No variables yet
Step 1 / 7
Looping through a dictionarypython
scores = {"Alice": 95, "Bob": 82, "Carol": 91}

for name, score in scores.items():
    print(f"{name}: {score}")

print(list(scores.keys()))    # ['Alice', 'Bob', 'Carol']
print(list(scores.values()))  # [95, 82, 91]
Dictionaries are perfect for counting. To count how often each item appears, use a dict where keys are items and values are counts. This is called the "frequency map" pattern.
๐ŸŒIn the Real World...

Every JSON API response is a dictionary. Every Kubernetes manifest you parse is nested dictionaries. Terraform state files, Docker inspect output, AWS API responses, all dicts.

๐ŸŽฏ

Phase Complete!

Data Structures

You know lists, tuples, dictionaries, and sets. For each one, name a real-world use case in DevOps or cloud engineering where that structure is the best choice.

0/500

Practice Exercises

0/2 solved
Exercise 1 of 2medium
โฑ 00:00

Word Frequency

Write a function word_count(sentence) that returns a dictionary with the count of each word.
Example: word_count("the cat sat on the mat") โ†’ {"the": 2, "cat": 1, "sat": 1, "on": 1, "mat": 1}
solution.py
1 / 2
Exercise 2 of 2medium
โฑ 00:00

Group by First Letter

Write a function group_by_first(words) that groups words by their first letter into a dictionary.
Example: group_by_first(["apple","avocado","banana","blueberry","cherry"])โ†’ {"a": ["apple","avocado"], "b": ["banana","blueberry"], "c": ["cherry"]}
solution.py
2 / 2
Solve all 2 exercises to unlock completion