Filtering Comprehensions with Conditions

🏷️ Lists and List Operations / List Comprehensions

Context: As you begin working with Python, you'll often need to extract specific items from a list based on certain conditions. Instead of writing long loops with if-statements, Python offers a clean, readable way to filter data: filtering comprehensions. This technique lets you combine list creation with condition checking in a single, elegant line of code.


⚙️ What is a Filtering Comprehension?

A filtering comprehension is a list comprehension that includes a condition at the end. It follows this pattern:

  • Basic structure: [expression for item in list if condition]
  • The condition acts as a gatekeeper — only items that evaluate to True are included in the new list
  • The expression can be the item itself, or a transformed version of it

Simple example: To get only even numbers from a list: - Original list: numbers = [1, 2, 3, 4, 5, 6] - Filtering comprehension: [n for n in numbers if n % 2 == 0] - Result: [2, 4, 6]


🕵️ How Conditions Work in Comprehensions

The condition in a filtering comprehension can be any expression that returns True or False. Here are common types of conditions:

  • Comparison operators: >, <, >=, <=, ==, !=
  • Membership tests: in, not in
  • String methods: .startswith(), .endswith(), .isalpha()
  • Custom functions: Any function that returns a boolean value

Example with string filtering: - Original list: fruits = ["apple", "banana", "avocado", "cherry", "apricot"] - Filter for fruits starting with "a": [f for f in fruits if f.startswith("a")] - Result: ["apple", "avocado", "apricot"]


📊 Comparison: Traditional Loop vs. Filtering Comprehension

Aspect Traditional Loop Filtering Comprehension
Code length 4-5 lines 1 line
Readability Clear but verbose Concise and direct
Performance Slower Faster (optimized in C)
When to use Complex logic or side effects Simple filtering tasks

Traditional loop example: - Create an empty list: even_numbers = [] - Loop through numbers: for n in numbers: - Check condition: if n % 2 == 0: - Append to list: even_numbers.append(n)

Filtering comprehension equivalent: - One line: even_numbers = [n for n in numbers if n % 2 == 0]


🛠️ Practical Filtering Examples

Filtering numeric data: - Get values above a threshold: [x for x in data if x > 100] - Get values within a range: [x for x in data if 10 <= x <= 50] - Get non-zero values: [x for x in data if x != 0]

Filtering text data: - Get strings longer than 5 characters: [word for word in words if len(word) > 5] - Get strings containing a specific substring: [item for item in items if "error" in item.lower()] - Get non-empty strings: [s for s in strings if s]

Filtering with multiple conditions: - Combine conditions with and: [x for x in data if x > 0 and x < 100] - Combine conditions with or: [x for x in data if x < 10 or x > 90] - Use not for negation: [x for x in data if not x % 2 == 0] (odd numbers)


🔄 Combining Filtering with Transformation

You can filter and transform items in the same comprehension:

  • Double only even numbers: [n * 2 for n in numbers if n % 2 == 0]
  • Capitalize long words: [word.upper() for word in words if len(word) > 5]
  • Extract first letter of fruits starting with 'a': [f[0] for f in fruits if f.startswith("a")]

Example breakdown: - Input: numbers = [1, 2, 3, 4, 5, 6] - Comprehension: [n ** 2 for n in numbers if n > 3] - Step 1: Filter items where n > 3[4, 5, 6] - Step 2: Apply expression n ** 2[16, 25, 36] - Final result: [16, 25, 36]


🎯 Common Pitfalls to Avoid

  • Forgetting the condition syntax: The if keyword comes after the for clause, not before
  • Using assignment in conditions: Conditions should only contain comparisons, not = (use == for equality)
  • Overcomplicating conditions: If your condition is too complex, consider using a helper function
  • Modifying the original list: Comprehensions create a new list — they don't modify the original

Incorrect example: - [x = x * 2 for x in data if x > 0] — this will cause a syntax error

Correct version: - [x * 2 for x in data if x > 0] — expression on the left, condition on the right


💡 Quick Tips for Engineers

  • Start with simple conditions and add complexity gradually
  • Test your condition separately before putting it in a comprehension
  • Use filtering comprehensions for data cleaning tasks (removing None, empty strings, outliers)
  • Remember that filtering comprehensions work with any iterable, not just lists (tuples, strings, sets)
  • For very large datasets, consider using generator expressions (x for x in data if condition) to save memory

Next step: Practice by taking a list of server status codes and filtering for only successful responses (200-299 range), or extract all log entries containing the word "ERROR" from a list of log lines.


Filtering comprehensions let you create a new list by including only items from an existing list that satisfy a condition.

🔧 Example 1: Basic numeric filter — keep only even numbers

This example shows how to filter a list of numbers to keep only those divisible by 2.

numbers = [1, 2, 3, 4, 5, 6]
even_numbers = [n for n in numbers if n % 2 == 0]
print(even_numbers)

📤 Output: [2, 4, 6]


🔧 Example 2: Filter strings by length

This example shows how to keep only strings that are longer than 4 characters.

names = ["Al", "Bob", "Charlie", "Diana", "Eve"]
long_names = [name for name in names if len(name) > 4]
print(long_names)

📤 Output: ['Charlie', 'Diana']


🔧 Example 3: Filter and transform in one step

This example shows how to filter items and apply a transformation to the items that pass the condition.

scores = [55, 72, 88, 91, 64, 79]
passing_grades = [f"{score}% - Pass" for score in scores if score >= 70]
print(passing_grades)

📤 Output: ['72% - Pass', '88% - Pass', '91% - Pass', '79% - Pass']


🔧 Example 4: Filter using a condition on another list (zip)

This example shows how to filter one list based on a condition applied to a parallel list.

engineers = ["Alice", "Bob", "Charlie", "Diana"]
years_experience = [2, 5, 8, 3]
senior_engineers = [engineers[i] for i in range(len(engineers)) if years_experience[i] >= 5]
print(senior_engineers)

📤 Output: ['Bob', 'Charlie']


🔧 Example 5: Filter a list of dictionaries by a key value

This example shows how to filter a list of structured data (dictionaries) to find matching records.

projects = [
    {"name": "Bridge", "status": "complete"},
    {"name": "Tunnel", "status": "in_progress"},
    {"name": "Tower", "status": "complete"},
    {"name": "Dam", "status": "planning"}
]
completed_projects = [p["name"] for p in projects if p["status"] == "complete"]
print(completed_projects)

📤 Output: ['Bridge', 'Tower']


📊 Comparison: List Comprehension vs. Traditional For Loop

Feature List Comprehension Traditional For Loop
Lines of code 1 line 3–5 lines
Readability Compact, expressive Verbose but explicit
Performance Faster (C-optimized) Slower (Python loop)
Best for Simple filtering + transformation Complex logic or side effects

Context: As you begin working with Python, you'll often need to extract specific items from a list based on certain conditions. Instead of writing long loops with if-statements, Python offers a clean, readable way to filter data: filtering comprehensions. This technique lets you combine list creation with condition checking in a single, elegant line of code.


⚙️ What is a Filtering Comprehension?

A filtering comprehension is a list comprehension that includes a condition at the end. It follows this pattern:

  • Basic structure: [expression for item in list if condition]
  • The condition acts as a gatekeeper — only items that evaluate to True are included in the new list
  • The expression can be the item itself, or a transformed version of it

Simple example: To get only even numbers from a list: - Original list: numbers = [1, 2, 3, 4, 5, 6] - Filtering comprehension: [n for n in numbers if n % 2 == 0] - Result: [2, 4, 6]


🕵️ How Conditions Work in Comprehensions

The condition in a filtering comprehension can be any expression that returns True or False. Here are common types of conditions:

  • Comparison operators: >, <, >=, <=, ==, !=
  • Membership tests: in, not in
  • String methods: .startswith(), .endswith(), .isalpha()
  • Custom functions: Any function that returns a boolean value

Example with string filtering: - Original list: fruits = ["apple", "banana", "avocado", "cherry", "apricot"] - Filter for fruits starting with "a": [f for f in fruits if f.startswith("a")] - Result: ["apple", "avocado", "apricot"]


📊 Comparison: Traditional Loop vs. Filtering Comprehension

Aspect Traditional Loop Filtering Comprehension
Code length 4-5 lines 1 line
Readability Clear but verbose Concise and direct
Performance Slower Faster (optimized in C)
When to use Complex logic or side effects Simple filtering tasks

Traditional loop example: - Create an empty list: even_numbers = [] - Loop through numbers: for n in numbers: - Check condition: if n % 2 == 0: - Append to list: even_numbers.append(n)

Filtering comprehension equivalent: - One line: even_numbers = [n for n in numbers if n % 2 == 0]


🛠️ Practical Filtering Examples

Filtering numeric data: - Get values above a threshold: [x for x in data if x > 100] - Get values within a range: [x for x in data if 10 <= x <= 50] - Get non-zero values: [x for x in data if x != 0]

Filtering text data: - Get strings longer than 5 characters: [word for word in words if len(word) > 5] - Get strings containing a specific substring: [item for item in items if "error" in item.lower()] - Get non-empty strings: [s for s in strings if s]

Filtering with multiple conditions: - Combine conditions with and: [x for x in data if x > 0 and x < 100] - Combine conditions with or: [x for x in data if x < 10 or x > 90] - Use not for negation: [x for x in data if not x % 2 == 0] (odd numbers)


🔄 Combining Filtering with Transformation

You can filter and transform items in the same comprehension:

  • Double only even numbers: [n * 2 for n in numbers if n % 2 == 0]
  • Capitalize long words: [word.upper() for word in words if len(word) > 5]
  • Extract first letter of fruits starting with 'a': [f[0] for f in fruits if f.startswith("a")]

Example breakdown: - Input: numbers = [1, 2, 3, 4, 5, 6] - Comprehension: [n ** 2 for n in numbers if n > 3] - Step 1: Filter items where n > 3[4, 5, 6] - Step 2: Apply expression n ** 2[16, 25, 36] - Final result: [16, 25, 36]


🎯 Common Pitfalls to Avoid

  • Forgetting the condition syntax: The if keyword comes after the for clause, not before
  • Using assignment in conditions: Conditions should only contain comparisons, not = (use == for equality)
  • Overcomplicating conditions: If your condition is too complex, consider using a helper function
  • Modifying the original list: Comprehensions create a new list — they don't modify the original

Incorrect example: - [x = x * 2 for x in data if x > 0] — this will cause a syntax error

Correct version: - [x * 2 for x in data if x > 0] — expression on the left, condition on the right


💡 Quick Tips for Engineers

  • Start with simple conditions and add complexity gradually
  • Test your condition separately before putting it in a comprehension
  • Use filtering comprehensions for data cleaning tasks (removing None, empty strings, outliers)
  • Remember that filtering comprehensions work with any iterable, not just lists (tuples, strings, sets)
  • For very large datasets, consider using generator expressions (x for x in data if condition) to save memory

Next step: Practice by taking a list of server status codes and filtering for only successful responses (200-299 range), or extract all log entries containing the word "ERROR" from a list of log lines.

Interactive Views

You are currently in 📚 All-in-One mode. Use the tabs at the top to switch to 📖 Theory Only or 💻 Code Only views.

Filtering comprehensions let you create a new list by including only items from an existing list that satisfy a condition.

🔧 Example 1: Basic numeric filter — keep only even numbers

This example shows how to filter a list of numbers to keep only those divisible by 2.

numbers = [1, 2, 3, 4, 5, 6]
even_numbers = [n for n in numbers if n % 2 == 0]
print(even_numbers)

📤 Output: [2, 4, 6]


🔧 Example 2: Filter strings by length

This example shows how to keep only strings that are longer than 4 characters.

names = ["Al", "Bob", "Charlie", "Diana", "Eve"]
long_names = [name for name in names if len(name) > 4]
print(long_names)

📤 Output: ['Charlie', 'Diana']


🔧 Example 3: Filter and transform in one step

This example shows how to filter items and apply a transformation to the items that pass the condition.

scores = [55, 72, 88, 91, 64, 79]
passing_grades = [f"{score}% - Pass" for score in scores if score >= 70]
print(passing_grades)

📤 Output: ['72% - Pass', '88% - Pass', '91% - Pass', '79% - Pass']


🔧 Example 4: Filter using a condition on another list (zip)

This example shows how to filter one list based on a condition applied to a parallel list.

engineers = ["Alice", "Bob", "Charlie", "Diana"]
years_experience = [2, 5, 8, 3]
senior_engineers = [engineers[i] for i in range(len(engineers)) if years_experience[i] >= 5]
print(senior_engineers)

📤 Output: ['Bob', 'Charlie']


🔧 Example 5: Filter a list of dictionaries by a key value

This example shows how to filter a list of structured data (dictionaries) to find matching records.

projects = [
    {"name": "Bridge", "status": "complete"},
    {"name": "Tunnel", "status": "in_progress"},
    {"name": "Tower", "status": "complete"},
    {"name": "Dam", "status": "planning"}
]
completed_projects = [p["name"] for p in projects if p["status"] == "complete"]
print(completed_projects)

📤 Output: ['Bridge', 'Tower']


📊 Comparison: List Comprehension vs. Traditional For Loop

Feature List Comprehension Traditional For Loop
Lines of code 1 line 3–5 lines
Readability Compact, expressive Verbose but explicit
Performance Faster (C-optimized) Slower (Python loop)
Best for Simple filtering + transformation Complex logic or side effects