Filter Elements Using List Comprehension in Python

In Python, list comprehension provides a concise way to filter elements from a list based on a condition. By using an if statement within list comprehension, we can efficiently create a new list containing only the elements that satisfy a given condition.


Examples

1. Filtering Even Numbers from a List

We can filter even numbers from a list using list comprehension and the modulo operator %.

</>
Copy
# List of numbers
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# Filtering even numbers using list comprehension
even_numbers = [num for num in numbers if num % 2 == 0]

# Printing the filtered list
print("Even Numbers:", even_numbers)

Explanation:

Here, we define a list called numbers containing integers from 1 to 10. Using list comprehension, we iterate through each element num and apply the condition num % 2 == 0. If the condition is True, the number is included in the new list even_numbers. This results in a list containing only even numbers.

Output:

Even Numbers: [2, 4, 6, 8, 10]

2. Filtering Words with More Than 4 Letters

We can filter words based on their length using list comprehension.

</>
Copy
# List of words
words = ["apple", "bat", "banana", "cat", "elephant"]

# Filtering words with more than 4 letters
long_words = [word for word in words if len(word) > 4]

# Printing the filtered list
print("Long Words:", long_words)

Explanation:

Here, we define a list words containing different strings. Using list comprehension, we iterate through each word and apply the condition len(word) > 4. If the length of the word is greater than 4, it is added to the new list long_words.

Output:

Long Words: ['apple', 'banana', 'elephant']

3. Filtering Numbers Greater Than a Certain Value

We can filter numbers greater than a specified value using list comprehension.

</>
Copy
# List of numbers
scores = [45, 67, 89, 23, 90, 50, 78]

# Filtering numbers greater than 60
high_scores = [score for score in scores if score > 60]

# Printing the filtered list
print("High Scores:", high_scores)

Explanation:

Here, we define a list scores containing numeric values. Using list comprehension, we iterate through each score and apply the condition score > 60. If the condition is met, the score is added to the new list high_scores.

Output:

High Scores: [67, 89, 90, 78]

4. Filtering Names That Start with a Specific Letter

We can filter names based on their starting letter using list comprehension.

</>
Copy
# List of names
names = ["Alice", "Bob", "Charlie", "David", "Eve"]

# Filtering names that start with 'A'
a_names = [name for name in names if name.startswith("A")]

# Printing the filtered list
print("Names starting with A:", a_names)

Explanation:

Here, we define a list names containing different names. Using list comprehension, we iterate through each name and apply the condition name.startswith("A"). If the name starts with the letter “A”, it is added to the new list a_names.

Output:

Names starting with A: ['Alice']

Conclusion

List comprehension provides an easy way to filter elements in Python. The key advantage is its readability and performance over traditional loops.

  1. Use if inside list comprehension to apply filtering conditions.
  2. Filter based on numerical conditions, string properties, or custom logic.
  3. List comprehension is often faster and more concise than using loops.