Lists, dictionaries and sets
By the end: You can choose the right container for a job and use it fluently.
- Lists: creating, adding, removing, sorting
- Dictionaries: keys and values
- Sets: unique items and fast membership checks
- Loops over each of them
- Comprehensions
New to python? A programming language is the set of written instructions a computer will follow, and Python is the one this field settled on. It reads close to English: a line that filters a list of records looks roughly like a sentence describing that filter. Every model provider ships a Python library, and nearly every machine learning tool is written in it. Why it matters
One value under one name gets you a short way. Real programs hold groups of things: a list of results, a record with named fields, a set of items already seen. This section covers Python's three main containers, how to loop over each, and how to choose between them, which usually comes down to whether order matters and whether you look things up by name or by position.
Lists#
A list holds items in order. It can hold duplicates. Square brackets.
topics = ["python", "sql", "git"]
topics[0] # 'python'
topics[-1] # 'git'
len(topics) # 3
topics.append("rag") # add to the end
topics.insert(0, "linux") # add at a position
topics.remove("sql") # remove by value
topics.pop() # remove and return the last item
"git" in topics # TrueSorting is worth knowing properly, because it comes up constantly.
nums = [5, 2, 9, 1]
sorted(nums) # [1, 2, 5, 9] returns a NEW list
nums.sort() # sorts nums in place, returns None
sorted(nums, reverse=True) # [9, 5, 2, 1]
# Sorting by something other than the value itself
people = [{"name": "A", "age": 30}, {"name": "B", "age": 25}]
sorted(people, key=lambda p: p["age"])A very common bug
nums = nums.sort() sets nums to None, because .sort() returns nothing. Either call nums.sort() on its own line, or use nums = sorted(nums).
Dictionaries#
A dictionary maps a key to a value. Think of a real dictionary: you look up a word and get its meaning. Curly braces.
user = {"name": "Jack", "role": "engineer", "years": 5}
user["name"] # 'Jack'
user["city"] = "Berlin" # add a new key
del user["years"] # remove a key
"role" in user # True
len(user) # number of keysLooking up a key that does not exist raises an error. get() returns None instead, or a default you choose. Use get whenever the key might be missing.
user["city"] # KeyError if missing
user.get("city") # None if missing
user.get("city", "n/a") # 'n/a' if missingLooping over a dictionary gives you the keys unless you ask for more.
for key in user:
print(key)
for key, value in user.items():
print(key, value)
user.keys() # all keys
user.values() # all valuesSets#
A set holds unique items with no order. Adding something twice changes nothing. Its main use is removing duplicates and checking membership quickly.
tags = {"python", "ai", "python"} # {'python', 'ai'}
tags.add("sql")
tags.discard("ai")
a = {1, 2, 3}
b = {2, 3, 4}
a & b # {2, 3} in both
a | b # {1, 2, 3, 4} in either
a - b # {1} in a only
# Removing duplicates from a list
unique = list(set([1, 2, 2, 3, 3, 3]))Sets matter for speed. Checking whether something is in a list means looking at every item one by one. Checking a set is instant, no matter how big it is. If you are checking membership inside a loop, use a set.
seen = ["a1", "b2", "c3"] # slow to search when large
seen = {"a1", "b2", "c3"} # fast, always
if "b2" in seen:
...Which one should you use?#
- Order matters and duplicates are fine, use a list.
- You look things up by a name or id, use a dictionary.
- You need uniqueness, or you check membership a lot, use a set.
- The values are fixed and must never change, use a tuple.
Comprehensions#
A comprehension builds a list in one line. It does the same job as a loop with append, and Python programmers expect to see it.
# The long way
squares = []
for n in range(5):
squares.append(n * n)
# The same thing
squares = [n * n for n in range(5)] # [0, 1, 4, 9, 16]You can filter at the same time by adding an if at the end.
nums = [1, 2, 3, 4, 5, 6]
evens = [n for n in nums if n % 2 == 0] # [2, 4, 6]
names = [u["name"] for u in users]
adults = [u for u in users if u["age"] >= 18]
# Dictionary and set comprehensions work the same way
by_id = {u["id"]: u for u in users}
roles = {u["role"] for u in users}Know when to stop
Comprehensions are good for one loop and one condition. Once you need nested loops and several conditions, a normal for loop is easier to read and nobody will criticise you for it.
Where people go wrong
Modifying a list while looping over it. Removing items mid-loop makes Python skip elements, because the positions shift underneath the loop. Build a new list instead, usually with a comprehension.
If this comes up in an interview
How would you remove duplicates from a list?
list(set(items)) is the quickest way, but it loses the original order. If order matters, use list(dict.fromkeys(items)), because dictionaries keep insertion order in modern Python.
Why is checking membership in a set faster than in a list?
A list has to compare against each item in turn, so the time grows with its length. A set uses a hash table, so it jumps straight to where the item would be. That takes the same time regardless of size.
What happens if you use a list as a dictionary key?
It raises a TypeError. Keys must be hashable, which means they cannot change. Lists can change, so they are not hashable. Use a tuple instead.
What is the difference between sorted(x) and x.sort()?
sorted returns a new sorted list and leaves the original alone. .sort() sorts the list in place and returns None. Assigning the result of .sort() to a variable is a common bug, because you get None.
Build this: Given a list of dictionaries representing people with a name, age and city, write code that returns the names of everyone over 25, grouped into a dictionary by city.
Watch it explained
This is one section of Python, 33 hours across 9 sections. Read it in full to get the whole topic in order.
Stop 1 of 50 on the AI Engineer route
0 of 50 topics done · 0h behind you, 603h to go
What you have marked done is stored in this browser only. It does not reach a server, and clearing site data will clear it.