all() Method in Python

for every element of x


The difference between all() and any() functions built into python

all(x) is for elements of x object, if all(x) argument x object all elements are not 0,", False or x is empty object, then return True, otherwise return False

Related course: Complete Python Programming Course & Exercises

all() syntax

The syntax of all() is:

all(iterable)
  • parameter must be an iteratable object

  • returns Return True if bool(x) is True for all values x in the iterable. If the iterable is empty, return True.

The Working of all() Method in Python

Actually all() works like this:

def all(iterable):
    for x in iterable:
        if bool(x) is False: # returns False whenever one is not satisfied
            return False
    return True

all() Function Usage And Examples

The examples below show you how both the function all() and any() work.

>>> #listlist, none of the elements are empty or 0
>>> all(['a', 'b', 'c', 'd']) 
True

>>> #listlist, there is an empty element
>>> all(['a', 'b', '', 'd']) 
False

>>> #listlist, there exists an element  for 0
>>> all([0, 1,2, 3]) 
False

>>> #tuple, none of the elements are empty or 0
>>> all(('a', 'b', 'c', 'd')) 
True

>>> #tuple, there is an empty element
>>> all(('a', 'b', '', 'd')) 
False

>>> #tuple, there exists an element  of 0
>>> all((0, 1, 2, 3)) 
False

>>> # empty list
>>> all([]) 
True

>>> # empty unit
>>> all(()) 
True

any(x) is to determine if x object is empty, if both are empty, 0, false, then return false, if not all are empty, 0, false, then return true

>>> #listlist, none of the elements are empty or 0
>>> any(['a', 'b', 'c', 'd']) 
True

>>> #listlist, there is an empty element
>>> any(['a', 'b', '', 'd']) 
True

>>> #tuple, there is an empty element
>>> any((0,1)) 
True

>>> #tuple, elements are empty
>>> any((0,'')) 
False

>>> # empty unit
>>> any(()) 
False

>>> # empty list
>>> any([]) 
False