Menu Close

Python all() function

In this python built-in functions tutorial, we are going to learn all about Python all() function along with various examples. This is the best built-in function to check an iterable contains valid items or not.

If the iterable contains valid items, it returns True otherwise returns False.

In the previous Python tutorial, we have seen all about the Python abs function to find the absolute value of a given number.

What is Python all() function?

Python all() function is a built-in function.all() function return True if any all items in an iterable are true otherwise return False.

Syntax

The syntax of all function in python is:-

any(iterable)

Parameter

all function in Python accepts only one parameter that is iterable.

  • iterable:– any iterable object ( list, tuple, dictionary, etc) which contains the items.

Return Value

Python all function returns:-

  • True:- If all the elements are iterable are True.
  • False:- If any element in an iterable is False.

Python all function example

Here we will take various examples to understand Python all function.

Example 1:

Check if any item in a tuple is True:

x = (True, 1, True)
x = all(x)
print(x)

Output will be:- True

Example 2:

Check if any item in a set is True:

x = (True, 1, True)
x = all(x)
print(x)

Output will be:- False

Example 3:

Check if any item in a dictionary is True:

z = {1 : "Apple", 1 : "Orange"}
x = all(z)
print(x)

Output will be:- True

Note:- When we use all() function with a dictionary, all function checks only keys of the dictionary not values.

Conclusion

In this tutorial, you have learned all about Python all() method to check all the items in iterable are true or not. all() function return only boolean value, True or False.

If this tutorial helped you, please keep visiting for further python built-in functions tutorials.

For more information:- Click Here

Python abs() function
Python callable() Function

Related Posts