In this tutorial, we will learn about the Python all[] function with the help of examples.
The all[]
function returns True
if all elements in the given iterable are true. If not, it returns False
.
Example
boolean_list = ['True', 'True', 'True']
# check if all elements are true
result = all[boolean_list]
print[result]
# Output: True
all[] Syntax
The syntax of the all[]
function is:
all[iterable]
all[] Parameters
The all[]
function takes a
single parameter:
- iterable - any iterable [list, tuple, dictionary, etc.] which contains the elements
all[] Return Value
all[]
function returns:
- True - If all elements in an iterable are true
- False - If any element in an iterable is false
All values are true | True |
All values are false | False |
One value is true [others are false] | False |
One value is false [others are true] | False |
Empty Iterable | True |
Example 1: How all[] works for lists?
# all values true
l = [1, 3, 4, 5]
print[all[l]]
# all values false
l = [0, False]
print[all[l]]
# one false value
l = [1, 3, 4, 0]
print[all[l]]
# one true value
l = [0, False, 5]
print[all[l]]
# empty iterable
l = []
print[all[l]]
Output
True False False False True
The all[]
function works in a similar way for tuples and sets like lists.
Example 2: How all[] works for strings?
s = "This is good"
print[all[s]]
# 0 is False
# '0' is True
s = '000'
print[all[s]]
s = ''
print[all[s]]
Output
True True True
Example 3: How all[] works with Python dictionaries?
In the case of dictionaries, if all keys [not values] are true or the dictionary is empty, all[] returns True. Else, it returns false for all other cases..
s = {0: 'False', 1: 'False'}
print[all[s]]
s = {1: 'True', 2: 'True'}
print[all[s]]
s = {1: 'True', False: 0}
print[all[s]]
s = {}
print[all[s]]
# 0 is False
# '0' is True
s = {'0': 'True'}
print[all[s]]
Output
False True False True True
❮ Built-in Functions
Example
Check if all items in a list are True:
mylist = [True, True, True]
x = all[mylist]
Try it Yourself »
Definition and Usage
The all[]
function returns True if all items in an iterable are true,
otherwise it returns False.
If the iterable object is empty, the all[]
function also returns True.
Syntax
Parameter Values
iterable | An iterable object [list, tuple, dictionary] |
More Examples
Example
Check if all items in a list are True:
mylist = [0, 1, 1]
x = all[mylist]
Try it Yourself »
Example
Check if all items in a tuple are True:
mytuple = [0, True, False]
x = all[mytuple]
Try it Yourself »
Example
Check if all items in a set are True:
myset = {0, 1, 0}
x = all[myset]
Try it Yourself »
Example
Check if all items in a dictionary are True:
mydict = {0 : "Apple", 1 :
"Orange"}
x = all[mydict]
Try it Yourself »
Note: When used on a dictionary, the all[] function checks if all the keys are true, not the values.
Related Pages
The any[] Function
❮ Built-in Functions
When coding in Python, have you ever had to check if any item or all items in an iterable evaluate to In this tutorial, we'll learn about Python's Before we jump into As shown in the snippet above, That said, it's fairly intuitive that all non-zero values, and non-empty iterables have a truth
value of Let's understand the syntax of the 👉 Syntax: Therefore, the Here are a few simple examples to verify how the Let's now use the List comprehensions can be very helpful in collecting all these truth values in a list. Here's a quick recap: As shown in the code snippet below, our example string Therefore,
calling Notice how For each character in the string, there's a corresponding truth value – Let's take another similar example. This time, let's check for the occurrence of letters in a string. True
?
The next time you need to do so, be sure to use the nifty functions any[]
and all[]
.any[]
and all[]
functions and use simple examples to understand how they work.The Boolean Data Type in Python
any[]
and all[]
, let's quickly revisit the Boolean data type in Python. You can call bool[]
on any Python object to get its truth value. You can run the code
examples below in your favorite IDE. # truth value of None is False
print[bool[None]]
# Output
False
# truth value of an empty string [""] is False
print[bool[""]]
# Output
False
# truth value of an empty list [or any iterable] is False
print[bool[[]]]
# Output
False
# truth value of 0 {int [0], float [0.0] and complex [0j]} is False
print[bool[0]]
# Output
False
None
has a truth value of False
,0
] – integer, floating point, and complex number representations of 0
– all have a truth value of False
, andFalse
.True
.How to Use the any[] Function in Python
any[]
function, look at some simple examples, and then proceed to more useful examples.any[iterable]
True
if bool[x]
is True
for any x
in the iterable.False
if the iterable is empty.any[]
function takes an iterable as the
argument, and returns True
so long as at least one of the items in the iterable is True
. any[]
function works:list_1 = [0,0,0,1,0,0,0,0]
# any[a list with at least one non-zero entry] returns True
print[any[list_1]]
# Output
True
list_2 = [0j,0,0,0.0,0,0,0.0,0]
# any[a list of zeros] returns False
print[any[list_2]]
# Output
False
list_3 = [True, False, False]
# any[a list with at least one True value] returns True
print[any[list_3]]
# Output
True
list_4 = ["","","code more"]
# any[a list with at least one non-empty string] returns True
print[any[list_4]]
# Output
True
list_5 = ["","",""]
# any[a list of empty strings] returns False
print[any[list_5]]
# Output
False
How to Use Python's any[] Function to Check for Digits in a String
any[]
function to check if there are any digits in a string. Let's write down the steps.isdigit[]
method on it.isdigit[]
returns True
if the character under test is a digit, else it returns False
. # List Comprehension
[output_expression for every_item in an_iterable]
|
|
V
result of doing something on each item in the iterable
# In essence, Loop through the iterable, do something on each item and
return the result of the operation.
coding**is**cool**345
contains digits. any[]
function on the string should return True
. We use list comprehension to get a list of True
and False
values depending on whether the character is a digit or not.my_string = "coding**is**cool**345"
are_there_digits = [char.isdigit[] for char in my_string]
print[any[are_there_digits]]
# Output
True
are_there_digits
is a list with as many items as the length of the string. True
if the character is a digit, and False
if the character is not a digit, as shown below.print[are_there_digits]
# Output
[False, False, False, False, False, False, False, False, False, False, False,
False, False, False, False, False, False, False, True, True, True]
How to Use Python's any[] Function to Check for Letters in a String
The string under test is ***456278]]]
which doesn't contain letters – calling any[]
returns False
as expected. For each character in the string, call the isalpha[]
method to check whether or not it is a letter.
my_string = "***456278]]]"
num = [char.isalpha[] for char in my_string]
print[any[num]]
# Output
False
The is_letter
is a
list of False
values, as verified below:
print[is_letter]
# Output
[False, False, False, False, False, False, False, False, False, False, False, False]
How to Use Python's any[] Function to Combine Multiple Conditions with Logical OR
Let's say you decide to be more productive and write down the list shown below. However, you choose not to be hard on yourself and decide that you can have lots of sweets so long as one of the items in the list happens!😀
Notice how we have multiple conditions to consider, but choose to have sweets even if one of them evaluates to True
.
Isn't this very similar to an if
statement where you need to check if multiple conditions chained by the logical or
operator evaluate to True
? Yes, it is and the any[]
function can come in really handy in doing that.
Suppose you have N
conditions c1
, c2
, c3
, ..., cN
. Consider the pseudocode below:
if c1 or c2 or ... c_[N-1] or CN:
# DO THIS
else:
# DO THIS
You can now collect all these conditions in an iterable, say, a list or a tuple, and then call any[]
on that iterable to check if one or more conditions are True
, as shown below. Isn't this simple? 😀
conditions = [c1,c2,..., c_N]
if any[conditions]:
# DO THIS
else:
# DO THIS
How to Use the all[] Function in Python
Let's start with the syntax of the all[]
function.
👉 Syntax: all[iterable]
- Returns
True
ifbool[x]
isTrue
for all valuesx
in the iterable. - Returns
True
if the iterable is empty.
The all[]
function takes an iterable as the argument, returns True
only if all items in the iterable evaluate to True
or if the iterable is empty. In all other cases, the all[]
function returns False
.
How to Use Python's all[] Function to Check for Letters in a String
Let's take similar examples to check for certain characteristics of strings.
The test string coding**is**cool
contains the special character *
in addition to letters. So, when we check if all characters in the string are letters by using the all[]
function, we should get False
.
my_string = "coding**is**cool"
are_all_letters = [char.isalpha[] for char in my_string]
print[all[are_all_letters]]
# Output
False
print[are_all_letters]
# Output
[True, True, True, True, True, True, False, False, True, True, False, False,
True, True, True, True]
Notice how the list are_all_letters
has False
values at all positions where the *
is present in our string.
How to Use Python's all[] Function to Check for Digits in a String
Let's now check if all characters in the string are digits by using the all[]
function. The test string 56456278
contains only digits, so, calling all[]
should return True
as the list comprehension gives us a list of True
values.
my_string = "56456278"
are_all_digits = [char.isdigit[] for char in my_string]
print[all[are_all_digits]]
# Output
True
print[are_all_digits]
# Output
[True, True, True, True, True, True, True, True]
How to Use Python's all[] Function to Combine Multiple Conditions with Logical AND
Let's consider the following example. This time, you're in contention for an iPad and the conditions are more stringent. You've got to complete all tasks in the list to get an iPad from your cousin.😀
Now, this is very similar to using an if
statement to check if multiple conditions chained by the logical and
operator evaluate to True
, as shown below:
if c1 and c2 and ... c_[N-1] and CN:
# DO THIS
else:
# DO THIS
You could use the all[]
function to make this all the more concise by collecting the conditions in an iterable, and then calling the all[]
function on the iterable.
conditions = [c1,c2,..., c_N]
if all[conditions]:
# DO THIS
else:
# DO THIS
Conclusion
I hope this tutorial helped you understand the any[]
and all[]
functions in
Python.
See you all soon in another post. Until then, happy learning!
Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started