Python all() Function with Syntax & Examples
The built-in library function all()
in Python returns True
if all the elements in the passed iterable are true (or if the iterable is empty) else it returns False
.
The iterable object (parameter value) can be lists, tuples, dictionaries etc.
all() Syntax
The syntax of all()
function is:
all(iterable)
all() Examples
Few examples for Python all()
function are:
# one value false, 0 is falsy value
>>> list1 = [0,200,30]
>>> print(all(list1))
False
# all value true
>>>list2 = [5, 10, 15, 20]
>>>print(all(list2))
True
# all value false
>>>list3 = [0, False, 0]
>>>print(all(list3))
False
# empty iterable
>>>list4 = []
>>>print(all(list4))
True
all() Parameters
The all()
function takes only one argument. which is:
- iterable - iterable object can be lists, tuples, dictionaries, etc.
all() Return Value
The all(iterable)
function returns:
- True — If all elements in an iterable are true or if the iterable is empty.
- False — If any element in an iterable is false.
Programs Using all()
Example 1: Python program for all() using tuple
Program
This program illustrates use of Python all()
function with tuples.
tup1 = (5, 1, False)
x = all(tup1)
print(x)
tup2 = ()
print(all(tup2))
mylist = (95,11,12)
x = all(mylist)
print(x)
Output
False True True
Example 2: Python program for all() using dictionaries
In 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.
Program
dict1 = {10: "False" , 100: "False"}
print(all(dict1))
dict2= {0 : “True”, 1: “True”}
print(all(dict2))
dict3 = { }
print(all(dict3))
Output
True False True
Example 3: Python program for all() using strings
This program illustrates use of Python all()
function with strings.
Program
str1 = "This is test one"
print(all(str1))
# 0 is False
# "0" is True
str2= "0"
print(all(str2))
str3 = ""
print(all(str3))
Output
True True True