Data Structures Jump to this section
Data structures let you group and organize values so your program can store and access collections of data.
This page covers the basic built-in types that are used the most: lists, tuples, dictionaries, and sets.
Lists (ordered, changeable) Jump to this section
Lists hold items in order and you can change them (add/remove/update). Use square brackets [].
You access items in a list by their position (called their "index"). The first item has an index of 0, the second an index of 1, and so on.
You use list_name[index] to access an item in the list.
foods = ["pizza", "taco", "sushi"]
print(foods[0]) # pizza, index (or position) starts at 0
foods.append("hamburger") # adds "hamburger" to the end of the list
foods[1] = "steak" # change element at index 1 ("taco") to "steak"
print(foods)
# ['pizza', 'steak', 'sushi', 'hamburger']
It is very important to remember that the first item in the list has an index of 0
Common list operations:
append(x)- addxto the endinsert(i, x)- insertxat indexi(does not replace anything)pop()- remove and return the last item (orpop(i)removes indexi)remove(x)- remove the first item equal toxlen(list)- number of items
Lists are mutable (they can be changed) - methods like append() modify the list.
Tuples (ordered, fixed) Jump to this section
Tuples are like lists but cannot be changed after creation (immutable). Use parentheses ().
coords = (10, 20)
print(coords[0]) # 10
# coords[0] = 5 # Error: tuples are immutable
Use tuples when you have a fixed collection of values (like coordinates) or want an unchangeable record.
Dictionaries (mappings) Jump to this section
Dictionaries store key/value pairs. Use curly braces {} and access with keys.
person = {"name": "John", "age": 30}
print(person["name"]) # John
person["age"] = 31
person["city"] = "Berlin" # add new key
Common dictionary operations:
d[key]- access value forkey(KeyError if missing)d.get(key, default)- same as d[key] except it will not give an error if the value is missing; it instead returnsdefaultif no value is found.
d.keys(),d.values(),d.items()- list keys, values, or pairsdel d[key]- remove a key
Dictionaries are mutable (can be changed) and are great for looking up values by name.
Sets (unordered, unique) Jump to this section
Sets hold unique items and are unordered. Use curly braces or set().
s = {1, 2, 3}
s.add(4)
print(2 in s) # True
Sets are useful for removing duplicates or checking whether an item is part of a group.
You cannot index a set (they are unordered), so s[1] would give an error.
Common set operations:
- A.union(B) - returns a set with the items from A and B combined
- A.intersection(B) - returns a set with the items that both A and B have
- A.difference(B) - returns a set with items that are in A but not in B
Mutability Jump to this section
- Mutable types (lists, dicts, sets) can be changed in place.
- Immutable types (tuples, strings, numbers) cannot be changed; assigning creates a new value.
Example difference:
a = [1, 2] # list, mutable
b = a
a.append(3)
print(b) # [1, 2, 3] - `b` sees the change because it references the same list
# If two variables point to the same list, changing one changes both.
x = (1, 2) # tuple, immutable
y = x
x = (1, 2, 3)
print(y) # (1, 2), y is referencing the original tuple
# Instead of changing the tuple, x is now pointing to a new tuple
Challenge Jump to this section
You are given this data:
menu = ["coffee", "tea", "sandwich", "cake"]
orders = {
"Carl": ["coffee", "cake"],
"Lily": ["tea", "sandwich", "cake"],
"Frank": ["coffee"]
}
-
List practice
- Add "cookie" to the menu
- Replace "tea" with "matcha"
- Print the updated menu
-
Dictionary practice
- Add a new order "Janet" that includes "coffee" and "cookie"
- Update Carl's order to include "sandwich"
- Print all customer names using a dictionary method
You don’t need anything new; everything here was covered above. Don't be afriad to look back at the list/dictionary operations if you need help.
List Hint
Use append() to add items to a list
The 2nd item in a list has index 1, so it would be accessed using list[1]
Dictionary Hint
In orders, each value is a list.
That means you can use list methods like append() on them.
You can use dictionary.keys(), dictionary.values(), and dictionary.items() to list only certain parts of a dictionary.
Show example solution
menu.append("cookie")
menu[1] = "matcha"
print(menu)
orders["Janet"] = ["coffee", "cookie"]
orders["Carl"].append("sandwich")
print(orders.keys())
Key takeaways Jump to this section
- Lists: ordered, mutable. Use for sequences you will change.
- Tuples: ordered, immutable. Use for fixed groups of values.
- Dictionaries: key → value mappings. Use for lookups by name.
- Sets: unordered, unique. Use for membership (whether an item belongs in it) and duplicate removal.
- Understand mutability: changing a mutable object affects all references to it.
