The all() is a built-in function in Python that returns a boolean result depending on the items of the passed iterable object.

It checks whether all elements in the iterable such as list, tuple, dictionary, etc are true or not. If yes then it returns True otherwise returns False.

Syntax:

all(iterable_object)

Python all() Parameters

iterable_object – Any iterable object with more than one element e.g list, dictionary, set, tuple, etc.

The all() function returns only True or False depending on the elements.

Python all() Examples

Example 1: List

>>> myList = [-1, 8, 4, 2]
>>> all(myList)
True
>>> myList = [-1, 8, 4, 2, 0]
>>> all(myList)
False

Any integer except 0 is considered True in Python.

Example 2: Dictionary

>>> mydict = {4: "A", 2: "B", 3: "C"}
>>> all(mydict)
True
>>> mydict = {4: "A", 2: "B", 3: "C", 0: "D"}
>>> all(mydict)
False

For a dictionary object, the all() function check keys instead of values.

Example 3: String

>>> myStr = "Pencil Programmer"
>>> all(myStr)
True
>>> myStr = "0 to 9"
>>> all(myStr)
True
>>> myStr = "0"
>>> all(myStr)
True

Though 0 in Python means False but with quotes i.e '0' it becomes a character that has a non-zero ASCII code, therefore, it results in True.

Leave a Reply