Just Enough Python I: the five fundamentals · Topic 3 Free
Data structures: lists, tuples, dicts, sets
The four containers you use daily. Dictionaries are the most important one for data work: they are how JSON looks in Python.
Data structures are how you hold data in memory. Four of them cover almost all data work.
Lists: ordered, changeable
cities = ["Berlin", "Toronto", "Seattle"]
cities.append("Vancouver") # add to the end
cities[0] # "Berlin" — first item, counting starts at 0
cities[-1] # "Vancouver" — last item
cities[1:3] # ["Toronto", "Seattle"] — slice, end not included
len(cities) # 4
"Berlin" in cities # True
A list is a column of values, a batch of rows, a queue of files to process. You will loop over lists constantly.
Tuples: ordered, fixed
point = (49.28, -123.12)
lat, lon = point # unpacking
A tuple is a list you cannot change. Use it for things that belong together and never change: coordinates, a (year, month) pair, a database row.
Dictionaries: key to value
The most important structure for data work. A dict is a set of named values. It is exactly how JSON looks when it lands in Python.
offer = {"company": "Shopify", "role": "Data Analyst", "salary_usd": 95000}
offer["company"] # "Shopify"
offer["remote"] = True # add a key
offer.get("bonus", 0) # 0 — safe read with a default, no error if missing
offer.keys() # the keys
offer.items() # (key, value) pairs, for looping
An API response is a dict. A config file is a dict. An Airflow task’s parameters are a dict. A list of dicts is a table:
offers = [
{"company": "Shopify", "salary_usd": 95000},
{"company": "Wealthsimple", "salary_usd": 110000},
]
offers[1]["company"] # "Wealthsimple"
Sets: unique values
countries = ["Canada", "Germany", "Canada", "Poland"]
set(countries) # {'Canada', 'Germany', 'Poland'}
len(set(countries)) # 3
Sets remove duplicates and test membership fast. set(a) & set(b) gives the common items, set(a) - set(b) the ones only in a. Handy for “which IDs are in the source but not in the target”.
Sets have no order. Never print a set when the order matters. Convert to a sorted list first: sorted(set(countries)).
Which one?
| Need | Use |
|---|---|
| A sequence you will add to or loop over | list |
| A fixed group of values that belong together | tuple |
| Named fields, JSON, config | dict |
| Unique values, membership tests | set |
Mutable vs immutable
Lists, dicts, and sets can change in place. Tuples and strings cannot. "abc".upper() returns a new string; the original is untouched. This trips up everyone once.
Practice
Real Python 3 runs in your browser. Solve the tasks, or just experiment.