Filtering Arrays Efficiently in Python
Combination of list comprehension or generator expression and all() function can be powerful mechanism for filtering values in arrays (lists, tuples, sets, dictionaries or strings).
all()
function explicitly indicates that we're verifying if a condition applies to every element in the iterable.- This can make the code more readable, especially when the condition involves a function calls or complex logic.
- It allows to encapsulate the condition inside a generator expression or iterable comprehension.
- Using
and
operator directly might be perceived as more concise in some cases but can be less readable if the condition is complex or involves function calls.
Filtering with One Condition
Suppose we need to filter out dictionary with students who have passed all their subjects, meaning they have scored at least 60 points in each subject.
students = [
{"name": "Alice", "scores": [75, 80, 92]},
{"name": "Bob", "scores": [55, 60, 47]},
{"name": "Charlie", "scores": [65, 70, 85]},
{"name": "Diana", "scores": [90, 93, 95]}
]
passing_students = [
student for student in students if all(score >= 60 for score in student['scores'])
]
print(passing_students)
# Output:
# {'name': 'Alice', 'scores': [75, 80, 92]}
# {'name': 'Charlie', 'scores': [65, 70, 85]}
# {'name': 'Diana', 'scores': [90, 93, 95]}
Filtering with Multiple Conditions
Let's say there's list of numbers and we want to filter out those which are both positive and even.
numbers = [-2, -1, 0, 1, 2, 3, 4, 5, 6]
filtered_numbers = [
num for num in numbers if all([num > 0, num % 2 == 0])
]
print(filtered_numbers)
# Output:
# [2, 4, 6]
We can do something similar with array of strings like here. Filter out strings in list with only alphanumeric characters using isalnum() function and whose length is greater than 5.
strings = [
'hello123', 'world', 'Python3.8', 'test', 'openAI', 'ChatGPT', 'code'
]
filtered_strings = [s for s in strings if all([s.isalnum(), len(s) > 5])]
print(filtered_strings)
# Output:
# ['hello123', 'openAI', 'ChatGPT']
In this snippet is a list of tuples where each tuple represents a point (x, y) in a 2D space. We want to filter out points that lie within a specific rectangle defined by coordinates.
points = [(1, 2), (3, 4), (5, 6), (7, 8), (9, 10), (4, 5)]
# Define the rectangle boundaries
x1, x2 = 2, 8
y1, y2 = 3, 7
# Using list comprehension and all() to filter points within the rectangle
filtered_points = [
point for point in points if all([x1 <= point[0] <= x2, y1 <= point[1] <= y2])
]
print(filtered_points)
# Output:
# [(3, 4), (5, 6), (4, 5)]
Here we use all()
to filter out only tuples which elements values are more than three. For memory efficiency we can use generator expression.
lst = [(1, 3), (), (), (), (), (), (), (), (1, 3, 4), (), (), (10, 45)]
filtered_tuples_gen = (x for x in lst if x and all(i > 3 for i in x))
for tupl in filtered_tuples_gen:
print(tupl)
# Output:
# (10, 45)
Subscribe to my newsletter
Read articles from Shant Danielyan directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by