Python Any Function

In this post, we will discuss python any function with the help of relevant examples. we will also discuss how python looks for the boolean value of objects. Then we will also apply any() function to user-defined objects. So, without wasting any time let’s start with a definition.

Defination :

Any() is the builtin function that takes an iterable(eg list, tuple,dict, set) as an argument and returns True if any element inside the iterable is True else it returns false. It also returns False if iterable is empty

Syntax :

any(iterable)

Source Code

Basically any function behaves exactly like this.

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

Python Any Function Examples :

Now let’s try to understand python any function with the help of examples.

Example 1

print(any([True,True,True,True]))

print(any([False,False,False,False]))

print(any([True,True,True,False]))

print(any([False,False,False,True]))

print(any([]))

Save and run the above code. you should see the following output :

Output :

python any function code example 1 output

Explaination :

  • In the first case, all the elements of the list were True so it returns True.
  • In the second case, all the elements of the list were False so it returns False.
  • In the third case, 3 elements were True so it returns True.
  • In the fourth case, 1 element was True so it returns True.
  • In the last case, iterable was empty so it returns False.

Example 2

list1 = [0,'',(),{}]
print(any(list1))

list2 = [1,'',0,[]]
print(any(list2))

Output :

False
True

any() function behaves the same with other iterables like tuple, set.

But in the case of a dictionary, it checks if the key is True or not. Let’s look at the example.

Example 3

d = {0:"Anything",False:"anything"}
print(any(d))

d1 = {1:"Else",0:"le"}
print(any(d1))

d2 = {}
print(any(d2))

Output :

False
True
False

It returns True if any key of dict is True else returns False. It also returns False if dict is empty.

How Python Looks For Boolean Values of Object.

Here is the algorithm python uses when it looks for the boolean value of any object.

  • Python first checks if the object implements the __bool__ method.
  • If objects do not have __bool__ then it checks for the __len__ method.
  • if objects do not have both __bool__ and __len__ then all instances are considered True.

Based on the above algorithm this is how any() function behaves to user defined objects.

Example 4

class Myobj:
    pass

l = [Myobj()]
print(any(l))

It returns True as Myobj Object does not defines __bool__ or __len__ method.

Output :

True
class Myobj:
    def __init__(self,e):
        self.e = e

    def __bool__(self):
        print("Bool is called")
        return len(self.e) != 0

l = [Myobj("test")]
print(any(l))

It returns True after checking the __bool__ method.

Output :

Bool is called
True
class Myobj:
    def __init__(self,e):
        self.e = e

    def __bool__(self):
        print("len is called")
        return len(self.e) != 0

l = [Myobj("")]
print(any(l))

Output :

len is called
False

That’s the wrap for the python any function tutorial.

Also, learn about python all function.

Leave a Reply