Looping and List Comprehensions
15 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)Python makes list processing exceptionally readable. Instead of manual index counters, use enumerate() and zip(). For transforming data, list comprehensions are a signature Python superpower.
Iterating with enumerate()
enumerate() is a built-in function that walks over a list and gives each item together with its position, as (index, item) pairs. It saves you from keeping a counter by hand.
When you need both the index and the item during a loop, enumerate() yields (index, item) pairs automatically.
podium = ["Gold", "Silver", "Bronze"]for rank, medal in enumerate(podium, start=1): print(f"{rank}. {medal}")Example 1 · Numbering a ticket queue
tickets = ["Login bug", "Slow report", "Typo on invoice"]for number, title in enumerate(tickets, start=1): print(f"#{number} {title}")#1 Login bug #2 Slow report #3 Typo on invoice
What this code does
enumeratehands out a number and an item on every round.start=1makes the numbers begin at 1 instead of 0.
The console displays 1. Gold, 2. Silver, 3. Bronze without needing a manual counter.
Pairing lists with zip()
The zip() function pairs up corresponding elements from two or more iterables in lockstep.
students = ["Pooja", "Manish", "Fatima"]marks = [95, 82, 88]for name, score in zip(students, marks): print(f"{name}: {score}")Example 2 · Matching products with prices
products = ["Pen", "Notebook", "Bag"]prices = [20, 55, 400]for product, price in zip(products, prices): print(f"{product}: {price}")print(list(zip(products, prices)))Pen: 20
Notebook: 55
Bag: 400
[('Pen', 20), ('Notebook', 55), ('Bag', 400)]What this code does
zippairs the first items together, then the second items, and so on.list(zip(...))shows the pairs as a list of tuples.
Iteration stops as soon as the shortest iterable is exhausted.
List comprehensions
List comprehension is one line that builds a new list from another sequence: [expression for item in sequence if condition]. The if part is optional and keeps only matching items.
A list comprehension constructs a new list in a single readable line: [expression for item in iterable if condition].
numbers = [1, 2, 3, 4, 5, 6]evens_squared = [n ** 2 for n in numbers if n % 2 == 0]print(evens_squared)Example 3 · Prices in rupees, 10 or more
prices_usd = [10.0, 25.5, 4.0, 50.0]exchange_rate = 83.0
prices_inr = [round(price * exchange_rate, 2) for price in prices_usd if price >= 10.0]print(prices_inr)[830.0, 2116.5, 4150.0]
What this code does
- Filters prices >= 10.0 (excludes 4.0).
- Converts each qualifying price to INR and rounds to 2 decimal places.
Example 4 · Pass or fail labels
marks = [72, 35, 88, 41]results = ["pass" if m >= 40 else "fail" for m in marks]print(results)['pass', 'fail', 'pass', 'pass']
What this code does
- When every item needs a value, put
if ... else ...before thefor. - A filter that drops items goes after the
forinstead.
Filters only even numbers (2, 4, 6) and squares them to produce [4, 16, 36].
Next step
Check your understanding
9 quick questions