Syntax
The syntax of the all() function is:
all(iterable)
Here, "iterable" is the input iterable (like list, tuple, set, etc.) that you want to check.
Returns
The all() function returns:
1. True: If all elements in the iterable are true or the iterable is empty.
2. False: If any element in the iterable is false.
Python all() Function with Examples
Now let's see some examples of how to use the all() function with different data types.
Example 1: Working of all() with Lists
Python
list1 = [True, True, True]
print(all(list1))
list2 = [True, False, True]
print(all(list2))
list3 = []
print(all(list3))
Output
True
False
True
In the first example, list1 contains all true elements, so all(list1) returns True. In the second example, list2 contains one false element, so all(list2) returns False. In the third example, list3 is an empty list, so all(list3) returns True.
Example 2: Working of all() with Tuples
Python
tuple1 = (1, 2, 3)
print(all(tuple1))
tuple2 = (0, 1, 2)
print(all(tuple2))
tuple3 = ()
print(all(tuple3))
Output
True
False
True
In the first example, tuple1 contains all non-zero elements, which are considered true, so all(tuple1) returns True. In the second example, tuple2 contains one zero element, which is considered false, so all(tuple2) returns False. In the third example, tuple3 is an empty tuple, so all(tuple3) returns True.
Example 3: Working of all() with Sets
Python
set1 = {1, 2, 3}
print(all(set1))
set2 = {0, 1, 2}
print(all(set2))
set3 = set()
print(all(set3))
Output
True
False
True
Similar to the examples with lists and tuples, in the first example, set1 contains all non-zero elements, so all(set1) returns True. In the second example, set2 contains one zero element, so all(set2) returns False. In the third example, set3 is an empty set, so all(set3) returns True.
Example 4: Working of all() with Dictionaries
Python
dict1 = {1: 'a', 2: 'b', 3: 'c'}
print(all(dict1))
dict2 = {0: 'a', 1: 'b', 2: 'c'}
print(all(dict2))
dict3 = {}
print(all(dict3))
Output
True
False
True
When using all() with dictionaries, it checks the keys of the dictionary. In the first example, dict1 has all non-zero keys, so all(dict1) returns True. In the second example, dict2 has one zero key, so all(dict2) returns False. In the third example, dict3 is an empty dictionary, so all(dict3) returns True.
Example 5: Working of all() with Strings
Python
str1 = "Hello"
print(all(str1))
str2 = ""
print(all(str2))
str3 = "False"
print(all(str3))
str4 = "0"
print(all(str4))
Output
True
True
True
True
When using all() with strings, it checks if all the characters in the string are non-empty. In the first example, str1 has all non-empty characters, so all(str1) returns True. In the second example, str2 is an empty string, so all(str2) also returns True. In the third example, str3 contains the word "False", but since it is a non-empty string, all(str3) returns True. Similarly, in the fourth example, str4 contains the character "0", which is also considered non-empty, so all(str4) returns True.
Frequently Asked Questions
What happens if the iterable is empty?
If the iterable passed to the all() function is empty, it returns True.
Can the all() function be used with user-defined objects?
Yes, the all() function can be used with user-defined objects. The objects should have a truth value defined for them using the bool() or len() method.
What is the difference between the all() and any() functions?
The all() function returns True if all elements in the iterable are true, while the any() function returns True if at least one element in the iterable is true.
Conclusion
In this article, we learned about the all() function in Python. We discussed its syntax, return values, and saw examples of how to use it with different data types like lists, tuples, sets, dictionaries, and strings. The all() function is a useful tool when you need to check if all elements in an iterable satisfy a certain condition.
You can also practice coding questions commonly asked in interviews on Coding Ninjas Code360.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.