Python all() Function Tutorial

In this section, we will learn what the all() function is and how to use it in Python.

What is Python all() Function?

The Python all() function is used to check the items of an iterable object and see if the entire items are True or interpret as True!

For numbers, they are True except the number 0.

Also, if the target iterable object is empty, the all() function will return True as well.

Python all() Function Syntax:

all(iterable)

Python all() Function Parameters

The all() function takes one argument and that is a reference to an iterable object that we want to check its items.

Python all() Function Return Value

The return value of this method will be True if the entire items in an iterable object interpret as True.

Otherwise, even if one element of the target iterable object interpreted as False, the final result of this function will be False as well.

Again, this function returns the value True for empty iterable objects.

Example: all() function and dictionary

dictionary = {

"name":"John",

"lastName": "Doe",

"age": 200,

}

result = all(dictionary)

print(result);

Output:

True

Note that the all() function will check the keys of a dictionary to see if they interpret as False.

Python all() Function and Dictionary Example:

dic = {

False: "Jack"

}

print(all(dic))

Output:

False

Example: all() function and lists

list1 = [1,2,3,4,5,6,8,0]

result = all(list1)

print(result);

Output:

False

For this particular example, only because of the last item, which is 0, the final result of the all() function became False.

Example: all() function and string

list1 = ["","1"]//The first item of this list interprets as False

print(all(list1))

print(all(""))

print(all("0"))

print(all(" "))

print(all("John Doe"))

Output:

False

True

True

True

True

Note: calling the all(“”) returns false because this is an empty string (an empty iterable).

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies