Python all()
Returns True if every item in an iterable evaluates to True, otherwise, it returns False.
Syntax
all(iterable)
Example 1
As long as one element in the iterable is False, all() will return False.
my_list = [True, "hello", 17]print(all(my_list))# Output: Truemy_list = [False, "hello", 17]print(all(my_list))# Output: False
Example 2
If the iterable is empty, all() will return True.
my_list = []print(all(my_list))# Output: True
The integer 0 evaluates to False; however, all non-zero numbers and strings evaluate to True.
my_list = [4, 3, 2, 1, 0]print(all(my_list))# Output: Falsemy_list = [4, 3, 2, 1, "0"]print(all(my_list))# Output: True
Different Types of Iterables
all() can be used on any iterable, such as a list, set, string, dictionary, or tuple.
Lists
my_list = [1, 1, 0, True]print(all(my_list))# Output: False
This is False because the integer 0 is False.
Sets
my_set = {1, "False", True, 7}print(all(my_set))# Output: True
This is True because strings and non-zero integers are True.
Strings
my_string = "Python is more fun than Javascript"print(all(my_string))# Output: True
This is True because strings and non-zero integers are True.
Dictionaries
When all() is used with a dictionary, it evaluates the keys, not the values. That means, even if a value is False, all() will return True if all of the keys evaluate to True.
my_dict = {0: "zero", 1: "one", 2: "two"}print(all(my_dict))# Output: False
This is False because the first key, 0, is False.
Tuples
my_tuple = ("Heffalumps", "and", "Woozles")print(all(my_tuple))# Output: True
This is True because all items in the tuple are True.
Codebyte Example
The following example demonstrates the use of all() function.
Checking if all elements in a list satisfy a condition:
The all() function returns True if all elements in an iterable are considered “truthy” (i.e., they evaluate to True in a boolean context). Otherwise, it returns False.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.
- Includes 27 Courses
- With Professional Certification
- Beginner Friendly.95 hours