Skip to content

Collections: Lists, Dicts, Sets

Try This First: Before reading, try this in Python: fruits = ['apple', 'banana']; fruits.append('cherry'); print(fruits). What does the list look like after append?

Learn Your Way

Read Build Watch Test Review Visualize
You are here Projects Videos Quiz Flashcards Diagrams

Python has several ways to group multiple values together.

Visualize It

See how lists, dicts, and sets store data differently in memory: Open in Python Tutor

Lists — ordered, changeable, allows duplicates

fruits = ["apple", "banana", "cherry"]
fruits.append("date")     # Add to end
fruits[0]                 # "apple" (first item)
fruits[-1]                # "date" (last item)
len(fruits)               # 4
"banana" in fruits        # True

Use lists when: you have an ordered collection of similar items (scores, names, files).

Dictionaries — key-value pairs, insertion-ordered (Python 3.7+), changeable

person = {"name": "Alice", "age": 30}
person["name"]            # "Alice"
person["city"] = "Denver" # Add new key
person.get("salary")      # None (safe access, no error)

Use dicts when: you have labeled data (a person's details, configuration, lookup table).

Sets — unordered, no duplicates

colors = {"red", "blue", "green", "red"}
print(colors)             # {"red", "blue", "green"} — duplicate removed
colors.add("yellow")
"red" in colors           # True

Use sets when: you need unique values or want to check membership quickly.

Set operations — comparing groups

Sets really shine when you want to compare two groups. Imagine you are planning a party and have two guest lists:

friday_guests = {"Alice", "Bob", "Charlie", "Diana"}
saturday_guests = {"Charlie", "Diana", "Eve", "Frank"}

# Union — everyone invited to at least one party
all_guests = friday_guests | saturday_guests
print(all_guests)
# {"Alice", "Bob", "Charlie", "Diana", "Eve", "Frank"}

# Intersection — people coming to BOTH parties
both_days = friday_guests & saturday_guests
print(both_days)
# {"Charlie", "Diana"}

# Difference — people only coming Friday (not Saturday)
friday_only = friday_guests - saturday_guests
print(friday_only)
# {"Alice", "Bob"}

# Difference the other way — people only coming Saturday
saturday_only = saturday_guests - friday_guests
print(saturday_only)
# {"Eve", "Frank"}

Think of it this way: - | (union) means "combine everything" - & (intersection) means "only what overlaps" - - (difference) means "what is in the first group but not the second"

These three operations cover most real-world "compare two groups" problems — finding shared friends, overlapping skills on a resume, or items on one shopping list that are missing from another.

Tuples — ordered, unchangeable

point = (3, 5)
x = point[0]             # 3
y = point[1]             # 5
# point[0] = 10          # Error! Tuples cannot be changed

Use tuples when: you have a fixed group of values that should not change (coordinates, RGB colors).

Quick comparison

Feature List Dict Set Tuple
Syntax [1, 2, 3] {"a": 1} {1, 2, 3} (1, 2, 3)
Ordered Yes Yes* No Yes
Changeable Yes Yes Yes No
Duplicates Yes No (keys) No Yes
Access by Index Key N/A Index

*Dicts maintain insertion order in Python 3.7+ but are not indexed by position.

Common mistakes

Empty dict vs empty set:

empty_dict = {}     # This is a dict, NOT a set
empty_set = set()   # This is how you make an empty set

Modifying a list while iterating:

# Wrong
for item in items:
    items.remove(item)

# Right — build a new list
items = [item for item in items if item != "remove_me"]

Practice

Quick check: Take the quiz

Review: Flashcard decks Practice reps: Coding challenges


← Prev Home Next →