Comprehensions
A loop that builds a list is so common that Python has a shortcut for it: a list comprehension.
# with a loop
doubled = []
for n in [1, 2, 3]:
doubled.append(n * 2)
# the same, as a comprehension
doubled = [n * 2 for n in [1, 2, 3]] # [2, 4, 6]
Read it as: "a list of n * 2, for each n in the list".
Filtering
Add if at the end to keep only some items:
evens = [n for n in range(10) if n % 2 == 0] # [0, 2, 4, 6, 8]
Dictionary comprehensions
The same idea with curly braces and key: value builds a dictionary:
prices = {"bread": 1.5, "milk": 1.2}
with_tax = {item: round(p * 1.15, 2) for item, p in prices.items()}
# {"bread": 1.73, "milk": 1.38}
Use comprehensions when the logic fits comfortably on one line. If you need several steps, a normal loop is clearer.
Your task
Write each function as a single comprehension:
squares(n): the squares of 1 up to and includingn.long_words(words): the words longer than 4 letters, in upper case.name_lengths(names): a dictionary from each name to its length.