Python’s built-in any(x)
function takes one iterable as an argument x
such as a list, tuple, or dictionary. It returns True
if at least one of the elements in the iterable evaluates to True
using implicit Boolean conversion, otherwise it returns False
. If the iterable is empty, e.g., any([])
, it returns False
because the condition is not satisfied for any element.
Argument | x -> x1, x2, ..., xn | Iterable such as a list, tuple, or dictionary |
Return Value | bool(x1) or bool(x2) ... or bool(xn) | Returns True if any element evaluates to True using the bool() conversion function. It basically performs a logical or on the Boolean representations of the elements in the iterable. |
Interactive Code Shell
Consider the following interactive code snippet:
Exercise: Remove one element from the list so that the any()
function returns False
.
Hint: Only one element evaluates to True
.
Check out my new Python book Python One-Liners (Amazon Link).
If you like one-liners, you’ll LOVE the book. It’ll teach you everything there is to know about a single line of Python code. But it’s also an introduction to computer science, data science, machine learning, and algorithms. The universe in a single line of Python!
The book was released in 2020 with the world-class programming book publisher NoStarch Press (San Francisco).
Publisher Link: https://nostarch.com/pythononeliners
Example any() for Lists
The following code shows you how to use the any()
function on different lists.
# Boolean list with False value print(any([True, False, True, True])) # True # Boolean list without False value print(any([True, True])) # True # Integer list with 0 value print(any([1, 2, -1, 0])) # True # Integer list without 0 value print(any([1, 2, -1])) # True # Nested list with empty inner list print(any([[], [1, 2, 3]])) # True # Nested list with two empty inner lists print(any([[], []])) # False # Empty List print(any([])) # False
Example any() for Tuples
If you use the any()
function on tuples, it’ll return a Boolean value that indicates whether all tuple elements evaluate to True
.
print(any((1, 2, 3))) # True print(any((0, 0, 0))) # False print(any((False, 2==3, -1))) # True print(any((True, 3, 1!=1))) # True
Example any() for Dicts
The any()
function on dictionaries checks for the iterable of keys (not values) whether at least one key evaluates to True
. If this is the case, the return value is True
, otherwise it’s False
.
d = {'': 10000, 0: 5000, tuple(): 0} print(any(d)) # False d['x'] = 100000 print(any(d)) # True
Implementation
According to the official Python documentation, the any()
function is semantically equivalent to the following code snippet:
def any(iterable): for element in iterable: if element: return True return False
So, it goes over all elements in the iterable and uses the element as an if condition to check whether it evaluates to True
or False
. As soon as one True
element is detected, it aborts the loop and returns True
. This is an optimization called short circuiting and it means that only the first True
value is evaluated!
Python any() Function with For Loop
You can also dynamically create an iterable using a generator expression and pass it into the any()
function. This may be called an “
function with a for loop“.any
()
print(any(x**2 == 16 for x in range(10))) # True
You use the condition x**2 == 16
which holds only for x=4
. As you apply this expression for all x
values from 0 to 9 (included) by using the range()
function, it mostly returns False
. Due to short circuiting, the any()
function returns True
after evaluating the fifth element x=4
.
Summary
Python’s built-in any(x)
function takes one iterable as an argument x
such as a list, tuple, or dictionary.
It returns True
if at least one of the elements in the iterable evaluates to True
using implicit Boolean conversion, otherwise it returns False
. If the iterable is empty, any([])
returns False
because the condition is not satisfied for any element.
Where to Go From Here?
Enough theory. Let’s get some practice!
Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.
To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
You build high-value coding skills by working on practical coding projects!
Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?
🚀 If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.