Sponsored Reserved space — layout preview until AdSense is connected

Reference library

Lists & loops

Iterate, transform, and combine sequences with readable Python patterns.

3 matches
Sponsored Reserved space — layout preview until AdSense is connected
Lists & loops medium

List comprehension filter

Build a new list in one line by filtering and transforming items.

lists comprehension filter
Python
numbers = [1, 2, 3, 4, 5, 6, 7, 8]
doubled_evens = [n * 2 for n in numbers if n % 2 == 0]
print(doubled_evens)

names = ["ada", "linus", "guido"]
title_case = [name.title() for name in names if name]
print(title_case)
4 0 Open
Lists & loops easy

Enumerate with index

Loop with both index and value — cleaner than manual counters.

loops enumerate lists
Python
tasks = ["read docs", "write code", "run tests"]

for i, task in enumerate(tasks, start=1):
    print(f"{i}. {task}")
5 0 Open
Lists & loops easy

Zip two lists

Pair items from parallel lists and iterate them together.

lists zip loops
Python
names = ["Ada", "Grace", "Katherine"]
scores = [98, 95, 100]

for name, score in zip(names, scores):
    print(f"{name}: {score}")

print(list(zip(names, scores)))
3 0 Open

Browse by section

Each section groups closely related Python snippets.