Mathew K Analytics

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…

What you'll learn

Data

No separate download needed — the notebook creates or downloads everything it uses.

📓 Full notebook

Download .ipynb

Data 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])
['Mazda RX4', 'Datsun 710', 'Hornet 4 Drive', 'Duster 360']
4
Mazda RX4
Duster 360
car_models.append('Valiant')
print(car_models)
car_models.remove('Datsun 710')
print(car_models)
car_models[0] = 'Mazda RX4 Wag'
print(car_models)
['Mazda RX4', 'Datsun 710', 'Hornet 4 Drive', 'Duster 360', 'Valiant']
['Mazda RX4', 'Hornet 4 Drive', 'Duster 360', 'Valiant']
['Mazda RX4 Wag', 'Hornet 4 Drive', 'Duster 360', 'Valiant']
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))
[18.1, 18.7, 21.0, 21.4, 22.8]
[22.8, 21.4, 21.0, 18.7, 18.1]
20.4

Part 2: Dictionaries#

car = {'model': 'Mazda RX4', 'mpg': 21.0, 'cyl': 6, 'hp': 110}
print(car)
print(car['model'])
print(car['mpg'])
{'model': 'Mazda RX4', 'mpg': 21.0, 'cyl': 6, 'hp': 110}
Mazda RX4
21.0
car['transmission'] = 'manual'
print(car)
print(car.get('weight', 'unknown'))
print(car.keys())
print(car.values())
{'model': 'Mazda RX4', 'mpg': 21.0, 'cyl': 6, 'hp': 110, 'transmission': 'manual'}
unknown
dict_keys(['model', 'mpg', 'cyl', 'hp', 'transmission'])
dict_values(['Mazda RX4', 21.0, 6, 110, 'manual'])

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")
Mazda RX4: 21.0 mpg, 6 cylinders
Datsun 710: 22.8 mpg, 4 cylinders
Duster 360: 14.3 mpg, 8 cylinders

Part 3: Tuples#

dimensions = (17.02, 2.62)
print(dimensions)
print(dimensions[0])
qsec, weight = dimensions
print(qsec, weight)
(17.02, 2.62)
17.02
17.02 2.62
try:
    dimensions[0] = 99
except TypeError as e:
    print('Error:', e)
Error: 'tuple' object does not support item assignment

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))
{8, 4, 6}
3
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)
{'Fiat 128', 'Datsun 710'}
{'Fiat 128', 'Datsun 710', 'Merc 240D', 'Toyota Corolla'}
{'Merc 240D'}

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.

  1. What does print(car_models[-1]) show for car_models = ['Mazda RX4', 'Datsun 710', 'Duster 360']?

  2. What does car.get('weight', 'unknown') return if the dict car has no 'weight' key?

  3. What happens when you run dimensions[0] = 99 on the tuple dimensions = (17.02, 2.62)?

  4. What does len(set([6, 4, 8, 6, 4, 4, 8, 6])) return?

  5. Given a = {'x', 'y', 'z'} and b = {'y', 'z', 'w'}, what is a & b?

💻 Practice exercises

Try each one in the notebook or in your own editor before opening the solution.

  1. 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))
    
  2. 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'))
    
  3. Given a list of dicts cars (model, mpg, cyl), loop over it and print model: mpg mpg, and also build a set of the unique cylinder counts and print it.

    💡 Hint

    Loop with for car in cars and 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.