Back to the main roadmap | Previous lesson: Loops
Sometimes you need to store multiple values in a single variable.
Python provides several collection types for this:
- List → Ordered and changeable
- Tuple → Ordered but unchangeable
- Set → Unordered with unique values
The biggest differences are:
- Can you change it?
- Does order matter?
- Are duplicate values allowed?
A list is an ordered and changeable collection.
Think of it like a shopping list written in pencil—you can add, remove, or change items whenever you want.
fruits = ["apple", "banana", "cherry"]
print(fruits)['apple', 'banana', 'cherry']
Lists use square brackets [].
Each item has an index.
Indexes start at 0, not 1.
fruits = ["apple", "banana", "cherry"]
print(fruits[0])
print(fruits[1])
print(fruits[2])apple
banana
cherry
Negative indexes count from the end.
fruits = ["apple", "banana", "cherry"]
print(fruits[-1])
print(fruits[-2])cherry
banana
Remember:
-1→ Last item-2→ Second last-3→ Third last
Slicing extracts part of a list.
numbers = [10, 20, 30, 40, 50]
print(numbers[1:3])
print(numbers[:2])
print(numbers[2:])
print(numbers[:])[20, 30]
[10, 20]
[30, 40, 50]
[10, 20, 30, 40, 50]
list[start:stop]
- Includes
start - Stops before
stop
Lists are mutable, meaning they can be changed.
fruits = ["apple", "banana", "cherry"]
fruits[0] = "mango"
print(fruits)['mango', 'banana', 'cherry']
fruits = ["apple", "banana"]
fruits.append("cherry")
fruits.insert(1, "kiwi")
fruits.remove("banana")
fruits.pop()
fruits.pop(0)
fruits.sort()
fruits.reverse()
print(len(fruits))
print("kiwi" in fruits)
fruits.clear()| Method | Description |
|---|---|
append() |
Add item to the end |
insert() |
Insert item at a specific position |
remove() |
Remove by value |
pop() |
Remove by index (default: last) |
sort() |
Sort the list |
reverse() |
Reverse the list |
len() |
Count items |
in |
Check membership |
clear() |
Remove all items |
This is a very common beginner mistake.
list_a = [1, 2, 3]
list_b = list_a
list_b.append(4)
print(list_a)[1, 2, 3, 4]
Both variables point to the same list.
list_a = [1, 2, 3]
list_b = list_a.copy()
list_b.append(4)
print(list_a)
print(list_b)[1, 2, 3]
[1, 2, 3, 4]
Use .copy() when you need an independent copy.
A shorter way to create lists.
squares = [x * x for x in range(5)]
print(squares)[0, 1, 4, 9, 16]
You'll learn list comprehensions in more detail later.
A tuple is an ordered but immutable collection.
Once created, it cannot be changed.
point = (10, 20)
print(point[0])
print(point[1])10
20
Tuples use parentheses ().
Tuples are useful when data should never change.
Examples:
- GPS coordinates
- RGB color values
- Days of the week
color = (255, 0, 0)point = (10, 20)
point[0] = 99TypeError: 'tuple' object does not support item assignment
Tuples cannot be modified.
point = (10, 20, 30)
print(len(point))
print(point[1:])
print(20 in point)
for value in point:
print(value)3
(20, 30)
True
10
20
30
A tuple can be unpacked into variables.
point = (10, 20)
x, y = point
print(x)
print(y)10
20
Functions often return tuples.
def get_dimensions():
return (1920, 1080)
width, height = get_dimensions()
print(width)
print(height)1920
1080
A comma creates a tuple.
not_a_tuple = (5)
actual_tuple = (5,)
print(type(not_a_tuple))
print(type(actual_tuple))<class 'int'>
<class 'tuple'>
A set is an unordered collection of unique values.
Duplicate values are removed automatically.
numbers = {1, 2, 3, 2, 1}
print(numbers){1, 2, 3}
Sets use curly braces {}.
Note: An empty set is created using:
set()Sets make removing duplicates easy.
names = ["Alex", "Sam", "Alex", "Jo", "Sam"]
unique_names = set(names)
print(unique_names){'Alex', 'Sam', 'Jo'}
Checking membership in a set is very efficient.
allowed_users = {"Alex", "Sam", "Jo"}
print("Alex" in allowed_users)True
Sets are unordered.
numbers = {1, 2, 3}
print(numbers[0])TypeError
You cannot access items by index.
a = {1, 2, 3}
a.add(4)
a.remove(2)
a.discard(99)| Method | Description |
|---|---|
add() |
Add an item |
remove() |
Remove an item (error if missing) |
discard() |
Remove if present (no error if missing) |
a = {1, 2, 3, 4}
b = {3, 4, 5, 6}
print(a | b)
print(a & b)
print(a - b)
print(a ^ b){1, 2, 3, 4, 5, 6}
{3, 4}
{1, 2}
{1, 2, 5, 6}
| Operator | Meaning |
|---|---|
| ` | ` |
& |
Intersection (common items) |
- |
Difference |
^ |
Symmetric Difference |
| Feature | List | Tuple | Set |
|---|---|---|---|
| Ordered | ✅ Yes | ✅ Yes | ❌ No |
| Changeable | ✅ Yes | ❌ No | ✅ Yes |
| Duplicate Values | ✅ Allowed | ✅ Allowed | ❌ Not Allowed |
| Brackets | [] |
() |
{} |
Create a file named lesson_collections.py.
# LISTS
fruits = ["apple", "banana", "cherry"]
fruits.append("mango")
fruits.insert(1, "kiwi")
print(fruits)
print(fruits[-1])
print(fruits[1:3])
list_a = [1, 2, 3]
list_b = list_a.copy()
list_b.append(4)
print("list_a:", list_a)
print("list_b:", list_b)
# TUPLES
point = (5, 10)
x, y = point
print(f"x is {x}, y is {y}")
def get_min_max(numbers):
return (min(numbers), max(numbers))
low, high = get_min_max([4, 7, 1, 9, 2])
print("low:", low, "high:", high)
# SETS
names = ["Alex", "Sam", "Alex", "Jo", "Sam", "Alex"]
unique_names = set(names)
print(unique_names)
math_club = {"Alex", "Sam", "Jo"}
art_club = {"Jo", "Priya", "Sam"}
print("Both clubs:", math_club & art_club)
print("Either club:", math_club | art_club)
print("Math only:", math_club - art_club)['apple', 'kiwi', 'banana', 'cherry', 'mango']
mango
['kiwi', 'banana']
list_a: [1, 2, 3]
list_b: [1, 2, 3, 4]
x is 5, y is 10
low: 1 high: 9
{'Alex', 'Sam', 'Jo'}
Both clubs: {'Jo', 'Sam'}
Either club: {'Alex', 'Sam', 'Jo', 'Priya'}
Math only: {'Alex'}
- Lists are ordered, changeable collections.
- Tuples are ordered but immutable.
- Sets store unique values and remove duplicates automatically.
- Lists support indexing, slicing, and many useful methods.
- Tuples support indexing and unpacking but cannot be modified.
- Sets do not support indexing because they are unordered.
- Use
.copy()to create an independent copy of a list. - Sets are excellent for removing duplicates and performing set operations like union and intersection.
Next Lesson: Dictionaries