What is the python equivalent of JavaScript’s Array.prototype.some / every?

Python has all(iterable) and any(iterable). So if you make a generator or an iterator that does what you want, you can test it with those functions. For example:

some_is_b = any(x == 'b' for x in ary)
all_are_b = all(x == 'b' for x in ary)

They are actually defined in the documentation by their code equivalents. Does this look familiar?

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

Leave a Comment