List Comprehension

Let’s explore list comprehensions in Python with simple explanations, emojis, step-by-step examples, and practice questions! 🚀📝

What is a List Comprehension? 🤔

A list comprehension is a concise way to create lists in Python. It lets you build a new list by applying an expression to each item in an iterable (like a list, string, or range), all in a single line!

Syntax:

[expression for item in iterable]
  • expression: What you want to do with each item (e.g., multiply by 2).
  • item: A variable name for each element.
  • iterable: The collection you loop over (list, range, etc.).

Basic Example 🏗️

Let’s make a list of squares for numbers 0 to 4:

squares = [x**2 for x in range(5)]
print(squares)  # Output: [0, 1, 4, 9, 16]

This is the same as using a for loop, but much shorter!12

With Condition (Filtering) 🧹

You can add an if condition to filter items:

evens = [x for x in range(10) if x % 2 == 0]
print(evens)  # Output: [0, 2, 4, 6, 8]

Only even numbers are included!32

Nested Loops in List Comprehensions 🔄

You can use more than one for in a comprehension (like nested loops):

pairs = [(x, y) for x in [1, 2] for y in [3, 4]]
print(pairs)  # Output: [(1, 3), (1, 4), (2, 3), (2, 4)]

This creates all combinations of x and y.2

Real-World Example: Remove Whitespace from Lines 📄

Suppose you have a list of strings with extra spaces:

lines = [" hello\n", " world\n", " python\n"]
clean = [line.strip() for line in lines]
print(clean)  # Output: ['hello', 'world', 'python']

Quickly cleans up every line!2

List Comprehension vs. For Loop ⚔️

TaskFor Loop ExampleList Comprehension Example
Squares of 0–4squares = []
for x in range(5):
squares.append(x**2)
squares = [x**2 for x in range(5)]
Filter evens from 0–9evens = []
for x in range(10):
if x % 2 == 0:
evens.append(x)
evens = [x for x in range(10) if x % 2 == 0]

List comprehensions are more concise and often faster!12

Advanced: Nested Comprehensions & Conditions 🌟

You can combine multiple for loops and if conditions:

results = [x*y for x in range(1, 4) for y in range(1, 4) if x != y]
print(results)  # Output: [2, 3, 2, 3, 4, 6]

All products of x and y, except when x equals y.2

Practice Questions & Solutions 📝

1️⃣ Create a list of cubes for numbers 0 to 5.

Show Solution
cubes = [x**3 for x in range(6)]
print(cubes)  # Output: [0, 1, 8, 27, 64, 125]

2️⃣ Make a list of all vowels in the string "hello world".

Show Solution
s = "hello world"
vowels = [ch for ch in s if ch in "aeiou"]
print(vowels)  # Output: ['e', 'o', 'o']

3️⃣ From the list [2, -3, 5, -7, 8], create a new list with only the positive numbers.

Show Solution
nums = [2, -3, 5, -7, 8]
positives = [n for n in nums if n > 0]
print(positives)  # Output: [2, 5, 8]

4️⃣ Create a list of all pairs (x, y) where x is from 1-3 and y is from a-c.

Show Solution
pairs = [(x, y) for x in range(1, 4) for y in ['a', 'b', 'c']]
print(pairs)
# Output: [(1, 'a'), (1, 'b'), (1, 'c'), (2, 'a'), (2, 'b'), (2, 'c'), (3, 'a'), (3, 'b'), (3, 'c')]

5️⃣ Given a list of words, create a new list with only words longer than 3 letters.

Show Solution
words = ["hi", "hello", "cat", "python", "sun"]
long_words = [w for w in words if len(w) > 3]
print(long_words)  # Output: ['hello', 'python']

Key Points with Emojis 🎯

  • List comprehensions are a compact, readable way to build lists 🏗️
  • Add if for filtering 🧹
  • Use multiple for for nested loops 🔄
  • Faster and cleaner than regular for loops ⚡
  • Can be used with any iterable (lists, strings, files, etc.)

If you want to know about set comprehensions or dictionary comprehensions, just ask! 😃

References: List comprehensions provide a concise syntax for creating lists and can include filtering and nested loops. They are often faster and more readable than traditional for loops312.


  1. python-handbook.pdf ↩︎ ↩︎ ↩︎

  2. Learning_Python.pdf ↩︎ ↩︎ ↩︎ ↩︎ ↩︎ ↩︎ ↩︎

  3. Introduction_to_Python_Programming_-_WEB.pdf ↩︎ ↩︎