Python all() Function

The all() function of Python is used where we need to check whether all the element of a list, tuple, dictionary, set etc. is True or not. That is, if any element is False, then the function all() returns False. Otherwise returns True. For example:

mylist = [True, True, True, True]
print(all(mylist))

produces True on output as shown in the snapshot given below:

python all function

and the following code, also uses all() function in Python:

mylist = [True, True, False, True]
print(all(mylist))

produces False, because one of its element is False. Here is the snapshot of the output:

all function in python

Note: All elements are referred as True by all() function, except the False keyword itself, and the value 0 as key of dictionary. Even empty list, tuple, dictionary, set etc. also returned as True by all() function.

all() Function with List

Here is an example uses, all() function of Python with list:

a = [1, 2, 3, 4, 5]
print(all(a))

b = ['p', 'y', 't', 'h', 'o', 'n']
print(all(b))

c = ["codes", "cracker"]
print(all(c))

d = [1, 3, True, 'p', "codes"]
print(all(d))

e = [1, 3, False, 'p', "codes"]
print(all(e))

f = []
print(all(f))

The output produced by above program are all True except the last statement's output as shown in the snapshot given below:

all function example python

Note: Empty list, tuple, dictionary, set etc. is also considered as True, by all() function.

all() Function with Tuple

To create and understand the use of all() function with tuple, using the same program as created above. Then only replace [ with ( and ] with ). Here is the complete program:

a = (1, 2, 3, 4, 5)
print(all(a))

b = ('p', 'y', 't', 'h', 'o', 'n')
print(all(b))

c = ("codes", "cracker")
print(all(c))

d = (1, 3, True, 'p', "codes")
print(all(d))

e = (1, 3, False, 'p', "codes")
print(all(e))

f = ()
print(all(f))

This program produces exactly same output as of previous program, that is, the program created in the section "all() function with list".

all() Function with Set

As done in previous program, to create a program that uses and demonstrates all() function with Set, using the same program as given above. We need to replace ( with { and ) with } as shown in the program given below:

a = {1, 2, 3, 4, 5}
print(all(a))

b = {'p', 'y', 't', 'h', 'o', 'n'}
print(all(b))

c = {"codes", "cracker"}
print(all(c))

d = {1, 3, True, 'p', "codes"}
print(all(d))

e = {1, 3, False, 'p', "codes"}
print(all(e))

f = {}
print(all(f))

This program too produces same output that will be

True
True
True
True
False
True

all() Function with Dictionary

Because, in case of dictionary, elements are in the form of key and value pair. Therefore, the function all() returns True/False only based on key's value. That is, if the value of all keys, does not contains False, then all() returns True. Otherwise, returns False. Here is an example:

a = {1: "one", 2: "two", 3: "three"}
print(all(a))

b = {'p': "python", "c": "codescracker"}
print(all(b))

c = {0: "name", 1: "address", 3: "contact"}
print(all(c))

d = {False: "Hi", True: "Hello"}
print(all(d))

e = {}
print(all(e))

Here is the output produced by above program:

python all function in dictionary

Note: The key equals to 0 also referred as False by all().

Python Online Test


« Previous Function Next Function »


Liked this post? Share it!