Lesson 4 · Data analytics zero to hero
Python Lists, Tuples, Sets & Dicts Explained | Data Analytics #4
Lists, dictionaries, tuples, and sets, the containers every real dataset ends up living in before pandas takes over. Builds on functions and control flow…
- CourseData analytics zero to hero
- Lesson4 of 30
- Video11 min
- FormatJupyter notebook · 10 code cells
What you'll learn
Data
No separate download needed — the notebook creates or downloads everything it uses.
📓 Full notebook
Download .ipynbData Analytics Zero to Hero, Video 4: Data Structures#
- Video four of the 30-part series: lists, dictionaries, tuples, and sets, the containers every real dataset ends up living in before pandas takes over.
- Builds on functions and control flow from videos two and three.
- Let's jump straight in.
Before You Start#
- Open a new Jupyter Notebook in VS Code and select your Python interpreter as the kernel.
Part 1: Lists#
car_models = ['Mazda RX4', 'Datsun 710', 'Hornet 4 Drive', 'Duster 360']
print(car_models)
print(len(car_models))
print(car_models[0])
print(car_models[-1])
car_models.append('Valiant')
print(car_models)
car_models.remove('Datsun 710')
print(car_models)
car_models[0] = 'Mazda RX4 Wag'
print(car_models)
mpg_values = [21.0, 22.8, 21.4, 18.7, 18.1]
print(sorted(mpg_values))
print(sorted(mpg_values, reverse=True))
print(sum(mpg_values) / len(mpg_values))
Part 2: Dictionaries#
car = {'model': 'Mazda RX4', 'mpg': 21.0, 'cyl': 6, 'hp': 110}
print(car)
print(car['model'])
print(car['mpg'])
car['transmission'] = 'manual'
print(car)
print(car.get('weight', 'unknown'))
print(car.keys())
print(car.values())
A List of Dictionaries#
cars = [
{'model': 'Mazda RX4', 'mpg': 21.0, 'cyl': 6},
{'model': 'Datsun 710', 'mpg': 22.8, 'cyl': 4},
{'model': 'Duster 360', 'mpg': 14.3, 'cyl': 8}
]
for car in cars:
print(f"{car['model']}: {car['mpg']} mpg, {car['cyl']} cylinders")
Part 3: Tuples#
dimensions = (17.02, 2.62)
print(dimensions)
print(dimensions[0])
qsec, weight = dimensions
print(qsec, weight)
try:
dimensions[0] = 99
except TypeError as e:
print('Error:', e)
Part 4: Sets#
cylinder_counts = [6, 4, 8, 6, 4, 4, 8, 6]
unique_cylinders = set(cylinder_counts)
print(unique_cylinders)
print(len(unique_cylinders))
four_cyl_models = {'Datsun 710', 'Merc 240D', 'Fiat 128'}
efficient_models = {'Datsun 710', 'Fiat 128', 'Toyota Corolla'}
print(four_cyl_models & efficient_models)
print(four_cyl_models | efficient_models)
print(four_cyl_models - efficient_models)
Wrap-Up: What You Learned#
- Lists: ordered and changeable, with indexing, append, remove, and sorting.
- Dictionaries: key-value pairs, plus a list of dictionaries as a mental model for a full dataset.
- Tuples: ordered but immutable, and how they relate to unpacking multiple return values.
- Sets: unique, unordered values, plus intersection, union, and difference.
- These four containers are the foundation pandas itself is built on top of. Video five covers reading and writing real CSV, text, and JSON files, so subscribe if you want it to land automatically.
🧠 Test yourself
Pick an answer to check it. Your best score is saved in this browser.
What does
print(car_models[-1])show forcar_models = ['Mazda RX4', 'Datsun 710', 'Duster 360']?Index -1 means the last item of a list.
What does
car.get('weight', 'unknown')return if the dictcarhas no 'weight' key?.get() returns the default you supply (here 'unknown') instead of raising an error when the key is missing.
What happens when you run
dimensions[0] = 99on the tupledimensions = (17.02, 2.62)?Tuples are immutable: you cannot assign to their items.
What does
len(set([6, 4, 8, 6, 4, 4, 8, 6]))return?A set keeps only unique values: {4, 6, 8}, which has 3 items.
Given
a = {'x', 'y', 'z'}andb = {'y', 'z', 'w'}, what isa & b?& is the intersection: only items that appear in both sets.
💻 Practice exercises
Try each one in the notebook or in your own editor before opening the solution.
Make a list
mpg_values = [21.0, 22.8, 21.4, 18.7, 18.1]. Print it sorted from high to low and print the average.💡 Hint
sorted(..., reverse=True) and sum()/len().
✅ Show solution
mpg_values = [21.0, 22.8, 21.4, 18.7, 18.1] print(sorted(mpg_values, reverse=True)) print(sum(mpg_values) / len(mpg_values))
Create a dict for one car with keys model, mpg and cyl. Add a 'transmission' key, then print the value of 'hp' safely, falling back to 'unknown'.
💡 Hint
Use .get('hp', 'unknown').
✅ Show solution
car = {'model': 'Mazda RX4', 'mpg': 21.0, 'cyl': 6} car['transmission'] = 'manual' print(car) print(car.get('hp', 'unknown'))
Given a list of dicts
cars(model, mpg, cyl), loop over it and printmodel: mpg mpg, and also build a set of the unique cylinder counts and print it.💡 Hint
Loop with
for car in carsand use set() on a list of cyl values.✅ Show solution
cars = [ {'model': 'Mazda RX4', 'mpg': 21.0, 'cyl': 6}, {'model': 'Datsun 710', 'mpg': 22.8, 'cyl': 4}, {'model': 'Duster 360', 'mpg': 14.3, 'cyl': 8}, {'model': 'Hornet 4 Drive', 'mpg': 21.4, 'cyl': 6}, ] for car in cars: print(f"{car['model']}: {car['mpg']} mpg") unique_cyl = set(car['cyl'] for car in cars) print(unique_cyl)
Found this useful?
All lessons, notebooks and datasets here are free. If they helped you, a coffee keeps new lessons coming.



