Search code examples
pythonif-statementsyntaxany

What does the builtin function any() do?


I did some google searching on how to check if a string has any elements of a list in it and I found this bit of code that works:

if any(i in string for i in list):

I know this works, but I don't really know why. Could you share some insight?


Solution

  • As the docs for any say:

    Return True if any element of the iterable is true. If the iterable is empty, return False. Equivalent to:

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

    So, this is equivalent to:

    for element in (i in string for i in list):
        if element:
            return True
    return False
    

    … which is itself effectively equivalent to:

    for i in list:
        element = i in string
        if element:
            return True
    return False
    

    If you don't understand the last part, first read the tutorial section on list comprehensions, then skip ahead to iterators, generators, and generator expressions.

    If you want to really break it down, you can do this:

    elements = []
    for i in list:
        elements.append(i in string)
    for element in elements:
        if element:
            return True
    return False
    

    That still isn't exactly the same, because a generator expression builds a generator, not a list, but it should be enough to get you going until you read the tutorial sections.


    But meanwhile, the point of having any and comprehensions and so on is that you can almost read them as plain English:

    if any(i in string for i in list): # Python
    
    if any of the i's is in the string, for each i in the list: # pseudo-English