all(iterable)
: ReturnsTrue
if all elements in the iterable are True; otherwise,False
.any(iterable)
: ReturnsTrue
if at least one element in the iterable isTrue
; otherwise,False
.
Key takeaways:
The
any()
function returnsTrue
if any element in an iterable isTrue
, otherwise it returnsFalse
.The
all()
function returnsTrue
if all elements in an iterable areTrue
, otherwise it returnsFalse
.
any()
is helpful for checking if at least one condition is satisfied, whileall()
checks that all conditions are satisfied.
Python's any()
and all()
functions are powerful tools for evaluating iterables in logical expressions. These functions simplify your code when working with multiple conditions, making it more readable and efficient. Understanding how to use them effectively can help you write cleaner and more concise code.
any()
?An iterable (such as a list, tuple, set, etc.) can be passed into the any()
function, which returns False
if all element evaluates to False
, and True
if at least one element evaluates to True
.
You can also check the documentation of the any()
function by using help()
, as shown below.
>>> help(any)
any()
do in Python?To determine whether any of the components are True
, simply pass an iterable to any()
.
one_truth = [True, False, False]three_lies = [0, '', None]print(any(one_truth))print(any(three_lies))
The first print statement prints True
because one of the elements in one_truth
is True
.
On the other hand, the second print statement prints False
because none of the elements are True
, i.e., all elements are False
.
any()
to check for digits in a stringThe any()
function checks if at least one character in the string is a digit. It returns True
if any character is a digit, otherwise False
.
# Checking for digits in a stringstring = "Hello123"result = any(char.isdigit() for char in string)print(result) # Output: True (since there are digits '1', '2', and '3' in the string)
any()
to check for letters in a stringThe any()
function checks if at least one character in the string is a letter. It returns True
if any character is a letter, otherwise False
.
# Checking for letters in a stringstring = "12345abc"result = any(char.isalpha() for char in string)print(result) # Output: True (since 'a', 'b', and 'c' are letters in the string)
any()
to combine multiple conditions with logical ORThe any()
function can be used to combine multiple conditions with logical OR. It returns True
if at least one condition is True
.
# Combining multiple conditions using `any()`string = "hello123"result = any([char.isdigit() for char in string]) or any([char.isupper() for char in string])print(result) # Output: True (since '1', '2', and '3' are digits, meeting one condition)
The code checks whether the string "hello123"
contains at least one digit or one uppercase letter using the any()
function.
Line 2: The variable string
is initialized with "hello123"
.
Line 3: The first condition, any(char.isdigit() for char in string)
, returns True
since the digits '1', '2', and '3'
are present. The second condition, any(char.isupper() for char in string)
, returns False
as there are no uppercase letters. Since the two conditions are combined with or
, the final result is True
. Using a generator inside any()
improves efficiency by stopping early when a True
value is found, avoiding the creation of an intermediate list.
Line 4: The boolean result
is printed.
all()
?Another Python function, all()
, accepts an iterable and returns True
if all of the elements evaluate to True
; if not, it returns False
.
all()
do in Python?Similar to any()
, all()
takes in a list, tuple, set, or any iterable, like so:
all_true = [True, 1, 'a', object()]one_true = [True, False, False, 0]all_false = [None, '', False, 0]print(all(all_true))print(all(one_true))print(all(all_false))
In Python, truthy values are those considered “true” in a boolean context (e.g., non-zero numbers, non-empty strings, objects). Falsy values are considered “false” (e.g., None
, False
, 0
, empty strings or collections).
The all()
function checks if all items in an iterable are truthy. If every item is truthy, it returns True
; otherwise, it returns False
.
all_true
: All values are truthy (True
, 1
, 'a'
, object()
), so all(all_true)
returns True
.
one_true
: Contains falsy values (False
, 0
), so all(one_true)
returns False
.
all_false
: All values are falsy (None
, ''
, False
, 0
), so all(all_false)
returns False
.
all()
to check for digits in a stringThe all()
function checks if every character in the string is a digit. It returns True
if all characters are digits, otherwise False
.
# Checking if all characters are digits in a stringstring = "12345"result = all(char.isdigit() for char in string)print(result) # Output: True (since all characters are digits)
all()
to check for letters in a stringThe all()
function checks if every character in the string is a letter. It returns True
if all characters are letters, otherwise False
.
# Checking if all characters are letters in a stringstring = "abcDEF"result = all(char.isalpha() for char in string)print(result) # Output: True (since all characters are letters)
all()
to combine multiple conditions with logical ANDThe all()
function can be used to combine multiple conditions with logical AND. It returns True
only if all conditions are True
.
# Combining multiple conditions using `all()`string = "hello"result = all([char.islower() for char in string]) and all([char.isalpha() for char in string])print(result) # Output: True (since all characters are lowercase letters)
The code checks whether all characters in the string "hello"
are lowercase letters and alphabetic using the all()
function.
Line 2: The variable string
is initialized with "hello"
.
Line 3: The first condition, all(char.islower() for char in string)
, returns True
since every character is lowercase. The second condition, all(char.isalpha() for char in string)
, also returns True
because all characters are alphabetic. Since both conditions are combined with and
, the final result is True
. Using a generator inside all()
improves efficiency by stopping early if a False
value is encountered, avoiding unnecessary iterations.
Line 4: The boolean result
is printed.
any()
and all()
?If you have many conditions that determine if a block of code should run, then any()
or all()
can make your code more readable.
For example, if you have a chain of or
conditions:
if condition1 or condition2 or condition3 or condition4:
# Do something here
...
You can use any()
like so:
conditions = (
condition1,
condition2,
condition3,
condition4
)
if any(conditions):
# Do something here
...
Similarly, if you have a long chain of and
conditions:
if condition1 and condition2 and condition3 and condition4:
# Do something here
...
You can use all()
to make it more readable:
conditions = (
condition1,
condition2,
condition3,
condition4
)
if all(conditions):
# Do something here
...
any()
and all()
for optimized multiple conditionsIn some cases, you may need to combine conditions using both any()
and all()
. This allows you to check multiple conditions with a more compact and optimized approach.
Let’s say you want to check if at least one character is a digit and all other characters are lowercase letters. Instead of using multiple loops, you can use a combination of any()
and all()
.
# Without optimization: manually checking multiple conditionsstring = "abc123"has_digit = Falseall_lower = Truefor char in string:if char.isdigit():has_digit = Trueif not char.islower():all_lower = Falsebreak # exit early if a non-lowercase letter is foundprint(has_digit and all_lower) # Output: False# Optimized version using `any()` and `all()`string = "abc123"result = any(char.isdigit() for char in string) and all(char.islower() for char in string)print(result) # Output: False
Before: The code uses two separate loops to check for digits and lowercase letters.
After: The code is optimized by combining the conditions into a single line using any()
for digits and all()
for lowercase checks, making the code more concise.
Learn the basics with our engaging course!
Start your coding journey with our “Learn Python” course, the perfect course for absolute beginners! Whether you’re exploring coding as a hobby or building a foundation for a tech career, this course is your gateway to mastering Python—the most beginner-friendly and in-demand programming language. With simple explanations, interactive exercises, and real-world examples, you’ll confidently write your first programs and understand Python essentials. Our step-by-step approach ensures you grasp core concepts while having fun along the way. Join now and start your Python journey today—no prior experience is required!
The any()
and all()
functions are invaluable tools for Python developers, offering a cleaner and more intuitive way to evaluate multiple conditions. Incorporating these functions into our code simplifies complex logical expressions, reduces redundancy, and enhances code maintainability.
Haven’t found what you were looking for? Contact Us