← Lesson
BitWithBite
Python · Quick Reference

Lesson 9 — Dictionaries & Sets Cheat Sheet

Python
In one line: A dictionary stores data as key-value pairs. Instead of accessing items by numeric index like a list, you access them by a meaningful key — a label you define. This makes dicts ...

Key Ideas

1What is a Dictionary?. A dictionary stores data as key-value pairs. Instead of accessing items by numeric index like a list, you access them by a meaningful key — a label you define. This ma...
2Iterating Dictionaries. Three views let you loop over different aspects of a dictionary. Each returns a special view object that always reflects the current state of the dict.
3Nested Dicts & Real Patterns. Dictionaries are the building block of almost all Python data — JSON APIs, database records, configuration files, and objects are all represented as nested dicts in pr...
4Sets — Unique Unordered Collections. A set is an unordered collection of unique items. Duplicate values are automatically discarded. Sets are incredibly fast for membership testing and are the go-to tool ...
5Choosing the Right Collection. Python's four main collection types each have a distinct purpose. Picking the right one for your data makes code cleaner, faster, and less error-prone.

Code Examples

# Creating a dictionary student = { "name" : "Fatima Malik", "age" : 20, "gpa" : 3.92, "active" : True } # Accessing values by key print(student["name"]) # Fatima Malik print(student["gpa"]) # 3.92 # .get() — safe access (no...
scores = {"Ali": 88, "Sara": 95, "Zara": 72} # Loop over keys for name in scores: print(name, end=" ") # Ali Sara Zara # Loop over values for score in scores.values(): print(score, end=" ") # 88 95 72 # Loop over key-value pairs — most common ...
# ── Nested dict — user record ───────────────── user = { "id" : 1001, "name" : "Ali Hassan", "address" : { "city" : "Lahore", "country": "Pakistan" }, "courses" : ["Python", "Web Dev"] } print(user["address"]["...