Bird
Raised Fist0
Pythonprogramming~5 mins

List comprehension with condition in Python - Cheat Sheet & Quick Revision

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is list comprehension with condition in Python?
It is a way to create a new list by looping over an existing list and including only items that meet a certain condition.
Click to reveal answer
beginner
How do you write a list comprehension that includes only even numbers from a list nums?
Use:
[x for x in nums if x % 2 == 0]
This means: take each x in nums only if x is even.
Click to reveal answer
beginner
What happens if the condition in a list comprehension is always false?
The result is an empty list because no items meet the condition to be included.
Click to reveal answer
intermediate
Can you use multiple conditions in a list comprehension? How?
Yes, by combining conditions with and or or. Example:
[x for x in nums if x > 0 and x % 2 == 0] selects positive even numbers.
Click to reveal answer
beginner
Rewrite this code using list comprehension with condition:
result = []
for x in nums:
    if x > 10:
        result.append(x * 2)
Using list comprehension:
[x * 2 for x in nums if x > 10]
Click to reveal answer
What does this list comprehension do?
[x for x in range(5) if x != 3]
ACreates a list of numbers from 0 to 4 excluding 3
BCreates a list of numbers from 0 to 3
CCreates a list with only the number 3
DCreates an empty list
Which of these is a correct syntax for list comprehension with condition?
A[if x > 0 for x in nums]
B[x if x > 0 for x in nums]
C[x for x in nums if x > 0]
D[x for if x > 0 in nums]
What will be the output of [x*2 for x in [1,2,3] if x > 2]?
A[2, 4, 6]
B[6]
C[4, 6]
D[]
How can you filter out odd numbers from a list nums using list comprehension?
A[x for x in nums if x < 0]
B[x for x in nums if x % 2 == 1]
C[x for x in nums if x > 0]
D[x for x in nums if x % 2 == 0]
What does the condition part in list comprehension control?
AWhich items are included in the new list
BHow many times the loop runs
CThe order of items in the list
DThe type of items in the list
Explain how to use list comprehension with a condition to create a list of squares of even numbers from another list.
Think about filtering first, then applying the square.
You got /3 concepts.
    Describe what happens if the condition in a list comprehension never matches any item.
    What does an empty filter mean for the output?
    You got /3 concepts.

      Practice

      (1/5)
      1. What does the following list comprehension do?
      [x for x in range(5) if x % 2 == 0]
      easy
      A. Creates a list of odd numbers from 0 to 4
      B. Creates a list of even numbers from 0 to 4
      C. Creates a list of numbers from 1 to 5
      D. Creates a list of numbers divisible by 3

      Solution

      1. Step 1: Understand the range and condition

        The range(5) generates numbers 0, 1, 2, 3, 4. The condition x % 2 == 0 selects numbers divisible by 2 (even numbers).
      2. Step 2: Apply the condition to each number

        From 0 to 4, the even numbers are 0, 2, and 4. These are included in the new list.
      3. Final Answer:

        [0, 2, 4] -> Option B
      4. Quick Check:

        Even numbers from 0 to 4 = [0, 2, 4] [OK]
      Hint: Look for the condition after 'if' to filter items [OK]
      Common Mistakes:
      • Confusing even and odd numbers
      • Ignoring the condition and taking all numbers
      • Misunderstanding the range function
      2. Which of the following is the correct syntax for a list comprehension that selects numbers greater than 10 from list nums?
      easy
      A. [x for x > 10 in nums]
      B. [x if x > 10 for x in nums]
      C. [if x > 10 for x in nums]
      D. [x for x in nums if x > 10]

      Solution

      1. Step 1: Recall list comprehension syntax

        The correct syntax is [expression for item in iterable if condition]. Here, expression and item are both x, iterable is nums, and condition is x > 10.
      2. Step 2: Check each option

        [x for x in nums if x > 10] matches the correct syntax. The other options have misplaced or missing parts.
      3. Final Answer:

        [x for x in nums if x > 10] -> Option D
      4. Quick Check:

        Correct list comprehension syntax = [x for x in nums if x > 10] [OK]
      Hint: Remember: for comes before if in list comprehension [OK]
      Common Mistakes:
      • Placing 'if' before 'for'
      • Omitting 'for' keyword
      • Using incorrect order of clauses
      3. What is the output of this code?
      nums = [3, 7, 12, 5, 20]
      result = [n*2 for n in nums if n > 6]
      print(result)
      medium
      A. [14, 24, 40]
      B. [3, 7, 12, 5, 20]
      C. [6, 14, 24, 10, 40]
      D. [12, 24, 40]

      Solution

      1. Step 1: Identify numbers greater than 6

        From the list, numbers greater than 6 are 7, 12, and 20.
      2. Step 2: Multiply each selected number by 2

        7*2=14, 12*2=24, 20*2=40. These form the new list.
      3. Final Answer:

        [14, 24, 40] -> Option A
      4. Quick Check:

        Filter >6 and double = [14, 24, 40] [OK]
      Hint: Apply condition first, then transform each item [OK]
      Common Mistakes:
      • Multiplying all numbers, ignoring condition
      • Including numbers less than or equal to 6
      • Confusing multiplication with addition
      4. Find the error in this list comprehension:
      values = [1, 2, 3, 4]
      new_values = [x for x in values if x > 2 else 0]
      medium
      A. SyntaxError due to incorrect use of else in list comprehension
      B. No error, code runs fine
      C. TypeError because of wrong condition
      D. NameError because x is undefined

      Solution

      1. Step 1: Understand list comprehension with condition

        In list comprehension, 'if' can be used to filter items, but 'else' must be part of a conditional expression, not after 'if' alone.
      2. Step 2: Identify the syntax error

        The code uses 'if x > 2 else 0' incorrectly. The correct way is to use a conditional expression inside the expression part, like [x if x > 2 else 0 for x in values].
      3. Final Answer:

        SyntaxError due to incorrect use of else in list comprehension -> Option A
      4. Quick Check:

        Else must be inside expression, not after if clause [OK]
      Hint: Use if-else inside expression, not after if clause [OK]
      Common Mistakes:
      • Placing else after if in list comprehension filter
      • Confusing filter condition with conditional expression
      • Ignoring Python syntax rules for comprehensions
      5. You have a list of strings: words = ['apple', '', 'banana', 'cherry', '', 'date']. Which list comprehension creates a new list with only non-empty strings in uppercase?
      hard
      A. [w for w in words if w.upper()]
      B. [w.upper() if w else w for w in words]
      C. [w.upper() for w in words if w]
      D. [w.upper() for w in words if w == '']

      Solution

      1. Step 1: Understand filtering non-empty strings

        In Python, empty strings are false in boolean context. Using if w filters out empty strings.
      2. Step 2: Convert filtered strings to uppercase

        For each non-empty string w, w.upper() converts it to uppercase.
      3. Final Answer:

        [w.upper() for w in words if w] -> Option C
      4. Quick Check:

        Filter non-empty and uppercase = [w.upper() for w in words if w] [OK]
      Hint: Use 'if w' to filter non-empty strings in list comprehension [OK]
      Common Mistakes:
      • Filtering empty strings incorrectly
      • Using if-else incorrectly inside comprehension
      • Not converting strings to uppercase