Day 7 45 min beginner

Data Structures - Dictionaries and Sets

Learn how to store key-value pairs and unique collections of data

Learning Objectives

  • Create and use Python dictionaries
  • Access, add, and remove dictionary items
  • Understand dictionary keys and values
  • Learn how to use Python sets for unique items
  • Perform basic set operations (union, intersection)

Key-Value Storage

Sometimes, you don’t want to store items in a numbered list. Instead, you want to label them. For example, a “user” has a “name”, an “email”, and an “age”. In Python, we use Dictionaries for this.

Python Dictionaries

A Dictionary is a collection which is ordered (as of Python 3.7), changeable, and does not allow duplicates. Dictionaries are written with curly braces {} and have keys and values.

dict_example.py
1
2
3
4
5
6
7
user = {
  "name": "Alice",
  "age": 25,
  "email": "alice@example.com"
}

print(user["name"]) # Output: Alice

Modifying Dictionaries

dict_mod.py
1
2
3
4
5
6
7
8
9
user = {"name": "Alice"}

# Add or update
user["age"] = 26
user.update({"email": "alice@new.com"})

# Remove
user.pop("age")
del user["name"]

Python Sets

A Set is an unordered collection with no duplicate elements. Sets are useful for removing duplicates from other collections and performing mathematical operations.

sets.py
1
2
3
4
5
6
7
8
9
colors = {"red", "green", "blue", "red"}
print(colors) # Output: {"green", "blue", "red"} (duplicates removed!)

# Basic Operations
a = {1, 2, 3}
b = {3, 4, 5}

print(a.union(b))        # {1, 2, 3, 4, 5}
print(a.intersection(b)) # {3}

Interactive Practice

Try creating a dictionary representing your favorite car (brand, model, year).

Feature Dictionary {k:v} Set {}
Access via Key No index/key
Mutable Yes Yes
Duplicates Keys: No, Values: Yes No
Order Ordered (3.7+) Unordered

Quiz

Complete this quiz with a minimum score of 80% to mark Day 7 as complete.

Loading quiz...

Discussion

Have questions or want to discuss this lesson? Join the conversation below!