all(iterable)
Checks if all specified elements are truthy.
Parameters
all() takes one parameter:
iterable- an iterable object (list, tuple, dictionary, etc.) containing elements
Return Value
all() returns:
True— if all elements in the iterable are truthyFalse— if any element in the iterable is false
Examples
How does all() work with lists?
# All values are truthy
list_ = [1, 3, 4, 5]
print(all(list_))
# All values are false
list_ = [0, False]
print(all(list_))
# One value is false
list_ = [1, 3, 4, 0]
print(all(list_))
# One value is truthy
list_ = [0, False, 5]
print(all(list_))
# Empty iterable
list_ = []
print(all(list_))
# Result:
# True
# False
# False
# False
# True
all() works similarly for tuples and sets, just like lists.
How does all() work with strings?
s = "Great!"
print(all(s))
# '0' is True
s = '000'
print(all(s))
s = ''
print(all(s))
# Result:
# True
# True
# True
How does all() work with Python dictionaries?
For dictionaries, if all keys (not values) are truthy or the dictionary is empty, all() returns True. Otherwise, it returns False.
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 True
s = {'0': True}
print(all(s))
# Result:
# False
# True
# False
# True
# True