Last updated: Apr 9, 2024
Reading timeยท5 min
Use the any()
function to check if any element in a list meets a
condition.
The any
function will return True
if any element in the list meets the
condition and False
otherwise.
my_list = [1, 3, 5, 15] if any(item > 10 for item in my_list): # ๐๏ธ this runs print('There is an item greater than 10') else: print('No items in the list are greater than 10') # ๐๏ธ True print(any(item > 10 for item in my_list)) # ๐๏ธ False print(any(item > 50 for item in my_list))
If you need to check if ALL elements in a list meet a condition, click on the following subheading:
The any function takes
an iterable as an argument and returns True
if any element in the iterable is
truthy.
We passed a generator expression to the any()
function.
In the example, we check if each item in the list is greater than 10
and
return the result.
You can use the assignment expression syntax if you need to get the element that meets the condition.
my_list = [1, 3, 5, 15] if any((match := item) > 10 for item in my_list): # ๐๏ธ this runs print('There is an item greater than 10') print(match) # ๐๏ธ 15 else: print('No items in the list are greater than 10')
Assignment expressions allow us to assign to variables within an expression
using the NAME := expression
syntax.
If the condition is met at least once, the any()
function returns True
.
any()
function is empty or none of the elements in the iterable are truthy, the any
function returns False
.my_list = [1, 3, 5] if any(item > 10 for item in my_list): print('There is an item greater than 10') else: # ๐๏ธ this runs print('No items in the list are greater than 10')
None of the items in the list is greater than 10
, so the condition is never
met and any()
returns False
.
If your list is empty, the any
function will always return False
.
my_list = [] if any(item > 10 for item in my_list): print('There is an item greater than 10') else: # ๐๏ธ this runs print('No items in the list are greater than 10')
Here is another example that uses the any()
function to check if at least 1
item in the list has a None
value.
my_list = ['a', 'b', None, 'd'] if any(item is None for item in my_list): # ๐๏ธ This runs print('There is a None value in the list') else: print('No items in the list have a value of None') # ๐๏ธ True print(any(item is None for item in my_list))
The generator expression we passed to the any()
function iterates over the
list and checks if each value is None.
If the condition is met at least once, the any
function returns True
,
otherwise, it returns False
.
You can also use a for
loop to check if any element in a list meets a
condition.
my_list = [1, 3, -4, 5, 15, -3] any_meets_condition = False for element in my_list: if element > 10: any_meets_condition = True print(element) # ๐๏ธ 15 break print(any_meets_condition) # ๐๏ธ True
We used a for
loop to iterate over the list
On each iteration, we check if the current element is greater than 10
.
If the condition is met, we set the any_meets_condition
variable to True
and
exit the loop.
You also have access to the element that meets the condition in the if
block.
Use the all()
function to check if all elements in a list meet a
condition.
The all
function will return True
if all elements in the list meet the
condition and False
otherwise.
my_list = [1, 3, 5, 15] if all(item > 0 for item in my_list): # ๐๏ธ this runs print('All elements in the list are greater than 0') else: print('Not all elements in the list are greater than 0') # ๐๏ธ True print(all(item > 0 for item in my_list)) # ๐๏ธ False print(all(item > 10 for item in my_list))
The all() built-in function
takes an iterable as an argument and returns True
if all elements in the
iterable are truthy (or the iterable is empty).
We passed a generator expression to the all()
function.
In the example, we check if all elements in the list are greater than 0
.
The all
function will return True
if all elements in the list meet the
condition and False
otherwise.
If a single value that doesn't meet the condition is encountered, the all()
function short-circuits returning False
.
for
loopYou can also use a basic for loop to check if all elements in a list meet a condition.
my_list = [1, 3, 5, 15] all_meet_condition = True for element in my_list: if element < 0: all_meet_condition = False break print(all_meet_condition) # ๐๏ธ True
We initialized a variable to True
and used a for
loop to iterate over the
list.
On each iteration, we check if the current item is less than 0
.
If the condition is met, we set the all_meet_condition
variable to False
and
break
out of the loop.
If the condition is never met, the all_meet_condition
variable remains set to
True
.
The break statement breaks out of the
innermost enclosing for
or while
loop.
You can also use this approach to get the elements that don't meet the condition or to get the ones that do.
my_list = [1, 3, -4, 5, 15, -3] all_meet_condition = True meet_condition = [] dont_meet_condition = [] for element in my_list: if element < 0: all_meet_condition = False dont_meet_condition.append(element) else: meet_condition.append(element) print(all_meet_condition) # ๐๏ธ False print(meet_condition) # ๐๏ธ [1, 3, 5, 15] print(dont_meet_condition) # ๐๏ธ [-4, -3]
We used a for
loop to iterate over the list.
On each iteration, we check if the current element is less than 0
.
If the condition is met, we append the value to the dont_meet_condition
list.
Otherwise, the value is appended to the meet_condition
list.
You can learn more about the related topics by checking out the following tutorials: