Dictionaries and Sets

In the last two lessons we covered lists and tuples, which are accessed "by position." But in real life, we often look things up "by name" — dictionaries by word, contacts by name. Dictionaries (dict) handle this. Sets are for mathematical set operations. Both are extremely useful in data processing.


1. What Is a Dictionary

A dictionary is defined with curly braces {}, containing a set of key-value pairs, separated by colons.

PYTHON
# Define a dictionary
student = {
    "name": "Alice",
    "age": 20,
    "city": "Beijing"
}

print(student)               # {'name': 'Alice', 'age': 20, 'city': 'Beijing'}
print(type(student))         # <class 'dict'>
💡 Think of a dictionary like a real dictionary: "keys" are the entry words, "values" are the definitions. Look up a key and instantly get its value. Dictionary lookups are extremely fast — whether there are 10 entries or 100,000, the speed is nearly the same.

Basic CRUD Operations

PYTHON
# Create a dictionary
user = {}

# Add / modify key-value pairs
user["name"] = "Alice"
user["age"] = 25
print(user)                 # {'name': 'Alice', 'age': 25}

# Modify an existing key
user["age"] = 26
print(user)                 # {'name': 'Alice', 'age': 26}

# Read a value
print(user["name"])         # Alice
# print(user["email"])      # KeyError! Non-existent key raises error

# Use get() for safe access — returns default if key doesn't exist
print(user.get("email"))             # None — not found, returns None
print(user.get("email", "not set"))  # not set — custom default

# Delete a key-value pair
del user["age"]
print(user)                 # {'name': 'Alice'}
⚠️ Note: Using dict["non_existent_key"] raises KeyError. Use get() to avoid this — it returns None or your specified default.

Example: Student Info Management (Difficulty ⭐)

PYTHON
# Store a student's full info in a dictionary
student = {
    "name": "Bob",
    "id": "2024001",
    "scores": {"Chinese": 85, "Math": 92, "English": 78},
    "is_active": True
}

# Read basic info
print(f"Name: {student['name']}")
print(f"ID: {student['id']}")

# Read from nested dictionary
math_score = student["scores"]["Math"]
print(f"Math score: {math_score}")

# Safe access with get
phone = student.get("phone", "not filled")
print(f"Phone: {phone}")
▶ Try it Yourself

Output:

TEXT
Name: Bob
ID: 2024001
Math score: 92
Phone: not filled

2. Common Dictionary Methods

Getting All Keys, Values, and Key-Value Pairs

PYTHON
user = {"name": "Alice", "age": 25, "city": "Beijing"}

print(user.keys())      # dict_keys(['name', 'age', 'city'])
print(user.values())    # dict_values(['Alice', 25, 'Beijing'])
print(user.items())     # dict_items([('name', 'Alice'), ('age', 25), ('city', 'Beijing')])

# Most common way to iterate a dictionary
for key, value in user.items():
    print(f"{key} = {value}")

Output:

TEXT
name = Alice
age = 25
city = Beijing

pop() and setdefault()

PYTHON
# pop() — remove and return the value for a key
user = {"name": "Alice", "age": 25, "city": "Beijing"}
city = user.pop("city")
print(f"Removed city: {city}")      # Removed city: Beijing
print(user)                          # {'name': 'Alice', 'age': 25}

# pop() can specify a default, avoiding KeyError
email = user.pop("email", "no email")
print(email)                         # no email — key doesn't exist, returns default

# setdefault() — if key exists, return its value; if not, insert the default
user = {"name": "Alice"}
result = user.setdefault("age", 18)
print(user)                          # {'name': 'Alice', 'age': 18}

# If key already exists, setdefault doesn't modify it
result = user.setdefault("name", "Bob")
print(user)                          # {'name': 'Alice', 'age': 18} — unchanged

update() — Merging Dictionaries

PYTHON
# Merge two dictionaries
defaults = {"theme": "light", "lang": "en", "font_size": 14}
custom = {"theme": "dark", "font_size": 16}

defaults.update(custom)    # custom overwrites matching keys in defaults
print(defaults)            # {'theme': 'dark', 'lang': 'en', 'font_size': 16}

Example: Word Count (Difficulty ⭐⭐)

PYTHON
# Count word occurrences in a sentence
text = "apple banana apple orange banana apple"

word_count = {}
for word in text.split():
    # If word is already in dict, increment; otherwise, set to 1
    word_count[word] = word_count.get(word, 0) + 1

print(word_count)          # {'apple': 3, 'banana': 2, 'orange': 1}

# Output with items()
for word, count in word_count.items():
    print(f"{word}: {count} times")
▶ Try it Yourself

Output:

TEXT
apple: 3 times
banana: 2 times
orange: 1 times

3. Dictionary Iteration and Nesting

Three Ways to Iterate a Dictionary

PYTHON
user = {"name": "Alice", "age": 25, "city": "Beijing"}

# Method 1: iterate over keys
for key in user:                # Equivalent to for key in user.keys():
    print(f"{key}: {user[key]}")

# Method 2: iterate over values
for value in user.values():
    print(value)

# Method 3: iterate over key-value pairs (recommended)
for key, value in user.items():
    print(f"{key} → {value}")

Nested Dictionaries

PYTHON
# Nested dictionary — storing multiple student records
students = {
    "1001": {"name": "Alice", "scores": [85, 90, 78]},
    "1002": {"name": "Bob", "scores": [92, 88, 95]},
    "1003": {"name": "Charlie", "scores": [76, 85, 82]},
}

# Iterate over nested dictionary
for sid, info in students.items():
    name = info["name"]
    avg = sum(info["scores"]) / len(info["scores"])
    print(f"{sid} {name}: Average {avg:.1f}")

Output:

TEXT
1001 Alice: Average 84.3
1002 Bob: Average 91.7
1003 Charlie: Average 81.0

4. What Is a Set

A set is defined with curly braces {} or set(). It looks like a dictionary but has no values, only keys. Sets are: unique elements, unordered.

PYTHON
# Define a set
fruits = {"apple", "banana", "orange", "apple"}    # Duplicates are auto-removed
print(fruits)            # {'orange', 'apple', 'banana'} — order may vary

# Create a set from a list
numbers = set([1, 2, 2, 3, 3, 3])
print(numbers)           # {1, 2, 3} — duplicates removed

# Empty set must use set(), not {} ({} is an empty dict)
empty_set = set()
print(type(empty_set))   # <class 'set'>
💡 The most common use of sets is deduplication: list(set(list_name)) quickly removes duplicates. But note that sets are unordered, so the original order is lost.

Basic Set Operations

PYTHON
# Add and remove
s = set()
s.add("apple")
s.add("banana")
s.add("apple")           # Duplicate add has no effect
print(s)                 # {'apple', 'banana'}

s.discard("apple")       # Remove; no error if not found
print(s)                 # {'banana'}

# s.remove("non_existent")   # Would raise KeyError!

5. Set Operations

This is where sets truly shine — mathematical intersection, union, and difference:

PYTHON
a = {1, 2, 3, 4, 5}
b = {4, 5, 6, 7, 8}

# Intersection — elements present in both sets
print(a & b)             # {4, 5}
print(a.intersection(b)) # Same

# Union — all elements from both sets (no duplicates)
print(a | b)             # {1, 2, 3, 4, 5, 6, 7, 8}
print(a.union(b))        # Same

# Difference — elements in a but not in b
print(a - b)             # {1, 2, 3}
print(a.difference(b))   # Same

# Symmetric difference — elements in a or b but not both
print(a ^ b)             # {1, 2, 3, 6, 7, 8}

Example: User Permission Analysis (Difficulty ⭐⭐⭐)

PYTHON
# Simulate social platform user relationships
all_users = {"Alice", "Bob", "Charlie", "Diana", "Eve"}
vip_users = {"Bob", "Diana", "Eve"}
blocked_users = {"Diana"}

# Find "normal users who are not VIP"
normal_users = all_users - vip_users - blocked_users
print(f"Normal users: {normal_users}")

# Find "active VIPs (not blocked)"
active_vip = vip_users - blocked_users
print(f"Active VIPs: {active_vip}")

# Find "blocked users who are VIP"
blocked_vip = vip_users & blocked_users
print(f"Blocked VIPs: {blocked_vip}")

# Find "all users who need notification (VIPs + blocked)"
notify_users = vip_users | blocked_users
print(f"Users to notify: {notify_users}")
▶ Try it Yourself

Output:

TEXT
Normal users: {'Alice', 'Charlie'}
Active VIPs: {'Bob', 'Eve'}
Blocked VIPs: {'Diana'}
Users to notify: {'Bob', 'Diana', 'Eve'}

Common Use Cases


❓ FAQ

Q When should I use a dictionary vs a list?
A If you're looking up data by "name" (key), use a dictionary — e.g., looking up a student by ID. If you just need to access data "sequentially," use a list — e.g., a shopping cart. Dictionary lookups are much faster than lists — the difference is significant with large datasets (thousands of items). ⚠️ Q: What restrictions do dictionary keys have? Why can't lists be keys? A: Dictionary keys must be immutable — numbers, strings, and tuples work. Lists are mutable, so they can't be keys. If a list was a key and its content changed, the dictionary wouldn't be able to find it. This is the same reason tuples can be keys — they're immutable, so they're safe. Q: What's the difference between discard() and remove()? A: remove() raises KeyError if the element doesn't exist. discard() doesn't — it removes if present, or silently does nothing if not. If you're unsure whether an element exists, use discard() for safety.

📖 Summary

  • Dictionaries use {}, store key-value pairs, and look up values by key quickly
  • Basic operations: dict[key] read/write, get() safe read, del delete
  • Common methods: keys(), values(), items() for iteration; pop() remove and return; update() merge
  • setdefault() returns existing value or inserts a default
  • Dictionary values can be any type, including lists and dicts — enabling nested structures
  • Sets use {} or set(), unique and unordered, mainly for deduplication and math operations
  • Set operations: & intersection, | union, - difference, ^ symmetric difference

📝 Exercises

  1. Basic (Difficulty ⭐): Create a dictionary for your personal info (name, age, city, hobby). Use get() to read a "job" field (return "not filled" if missing). Use items() to output all key-value pairs.

  2. Intermediate (Difficulty ⭐⭐): Given the sentence "hello world hello python world hello", count the occurrences of each word and find the most frequent word. Hint: Use a dictionary for counting; use max(dict.items(), key=lambda x: x[1]) to find the max.

  3. Challenge (Difficulty ⭐⭐⭐): Write a "Simple Contact Book" program. Use a dictionary (name → phone number). Support: add Alice 13800138000, del Alice, find Alice, all (list all). Use while True; exit to quit. Hint: Use split() to parse commands.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏