Lists, dicts and loops: the Python you use every day
The three structures that appear in almost every script and screening question.
Most Python interview questions for junior roles are about lists, dictionaries and the loops that walk through them. If you can read and write these fluently, you can handle the rest.
Lists
scores = [42, 38, 51]
scores.append(45) # add to the end
scores[0] # 42 (first item)
scores[-1] # 45 (last item)
scores[1:3] # [38, 51] (stop before index 3)
len(scores) # 4
Two names can point at the same list. b = a does not copy; b = a.copy() does. This is the single most common trick question.
Dictionaries
user = {"name": "Aisha", "wpm": 42}
user["wpm"] # 42
user.get("city", "n/a") # "n/a" — no KeyError
user["city"] = "Lagos" # add or update
for key, value in user.items():
print(key, value)
Keys are unique and must be immutable (strings, numbers, tuples). Values can be anything.
Loops and comprehensions
evens = [n for n in range(10) if n % 2 == 0] # [0, 2, 4, 6, 8]
squares = {n: n * n for n in range(4)} # {0: 0, 1: 1, 2: 4, 3: 9}
for i, s in enumerate(scores): # index and value together
print(i, s)
A comprehension is a loop that builds a collection in one line. Use it when the result is a new list or dict; use a normal loop when you need side effects such as printing or writing to a file.
The Python test has three questions built on exactly these: list aliasing, slicing and a comprehension. Try it after this lesson.
More in Programming