Python using any, all

P

any and all can be think of as a series of logical or and and operators.

any
any will return True when at-least one of the elements is True

all
all will return True only when all the elements are True

Internal structure of any(iterable)

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False

Internal structure of all(iterable)

def all(iterable):
    for element in iterable:
        if not element:
            return False
    return True

Example:

print(any(i == 'i' for i in 'asdfisadf'))
#return True

print(all(i == 'i' for i in 'iiia'))
#return False

About the author

Imran Pollob

Learn => Practice => Implement => Repeat

Add comment

Categories

Imran Pollob

Learn => Practice => Implement => Repeat

Get in touch