In this tutorial, we will learn about the python all() method and how we can use it.

Python all() Method

What is python all() method?

The all()  is a built-in function available in python, which will return True when all the elements in the given iterable are true. If any null value is present in iterable, it will return false.

Ann empty iterable will return True in any() function.

The syntax of all() function is :

all(iterable)

Python all() Parameters

The all() method will take a single argument as an iterable, including list, tuple, set, dictionary, string, and loop in python.

Let’s see an example of all() method.

Example 1: How to use all() in lists?

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

my_list = [0,False]
print(all(my_list))

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

my_list = [0,False,1]
print(all(my_list))

my_list = []
print(all(my_list))

Output:

True
False
False
False
True

The all() method will work similarly for types and sets like the list in python.

Example 2 : How to use all() on strings?

my_string = "This is Python"
print(all(my_string))

my_string = ""
print(all(my_string))

The output will be as follows.

True
True

Example 3: How to use all() on dictionaries?

my_dict = {1:True}
print(all(my_dict))

my_dict = {0:False}
print(all(my_dict))

my_dict = {0:'False',1:'True'}
print(all(my_dict))

my_dict = {0:'False'}
print(all(my_dict))

my_dict = {}
print(all(my_dict))

The Output will be as follows.

True
False
False
False
True

In dictionaries, it is all the keys, not the values are true, or the dictionary is empty, then all() will return True otherwise, it will return false for all other cases.

 

 

Was this article helpful?
Share on: