
List comprehension:
# Instead of:
squares = []
for x in range(10):
squares.append(x**2)
# Write:
squares = [x**2 for x in range(10)]
# With condition:
evens = [x for x in range(20) if x % 2 == 0]Dict comprehension:
word_lengths = {word: len(word) for word in ["cat", "elephant", "dog"]}
# {"cat": 3, "elephant": 8, "dog": 3}Set comprehension:
unique_lengths = {len(word) for word in ["cat", "dog", "elephant"]}
# {3, 8}Generators — memory-efficient iteration:
# List: loads ALL values into memory
big_list = [x**2 for x in range(1000000)]
# Generator: produces values one at a time
big_gen = (x**2 for x in range(1000000))
# Generator function:
def countdown(n):
while n > 0:
yield n
n -= 1Reference:
TaskLoco™ — The Sticky Note GOAT