AppDividend
Latest Code Tutorials

Python any() Function Example | any() Method Tutorial

0

Python any() Function Example | any() Method Tutorial is today’s topic. Python any() function returns True if any element of an iterable is True. If not, any() returns False. If the iterable object is empty, the any() function will return False. The any() function is the opposite of all() function. An iterator is an object that contains the countable number of values. Python iterator is an object that can be iterated upon, meaning that you can traverse through all the values inside an iterator.

Python any() Function

The syntax of any() is following.

any(iterable)

Python any() method takes an iterable in Python. Those iterables are Python List, Python Dictionary, or Python Tuple.

The any() method returns following values.

  1. True if at least one item of an iterable is True.
  2. False if all items are false or if an iterable is empty.

#Any() method with Python Dictionary

See the following code.

dictA = {0: 'False', 1: 'False'}
print(any(dictA))

dictB = {1: 'True', 21: 'True'}
print(any(dictB))

dictC = {1: 'True', False: 0}
print(any(dictC))

dictD = {}
print(any(dictD))

dictE = {'0': 'True'}
print(any(dictE))

dictF = {0: 'True'}
print(any(dictF))

See the following output.

➜  pyt python3 app.py
True
True
True
False
True
False
➜  pyt

In Dictionary, the any() function checks only dictionary keys and not the values. Values do not matter if they are True or False.

In the above examples, you can see that, if any dictionary key’s value is 1 or True then it returns True.

If either dictionary is empty or all the keys are either 0 or False, then it will return False.

#Any() method with Python List

Let’s take an example of Python List iterator with any() method.

# app.py

listA = ['Emilia Clarke', 'Millie Bobby Brown', 'Jennifer Aniston']
print(any(listA))

listB = []
print(any(listB))

listC = [1, 0]
print(any(listC))

listD = [0, False]
print(any(listD))

Now, see the output.

➜  pyt python3 app.py
True
False
True
False
➜  pyt

So, same as the dictionary, if any of the list values are 1 or True, or any value except 0 or False, then it will return True.

If all the list is empty or all the values are either 0 or False, then it returns False.

#Any() method with Python String

Let’s check the String iterator with any() method.

# app.py

strA = 'Millie Bobby Brown'
print(any(strA))

strB = ''
print(any(strB))

See the following output.

➜  pyt python3 app.py
True
False
➜  pyt

From the above string examples, we can say that if the string is empty, then it will return False otherwise it returns True.

#Any() method with Python Tuple

Let’s take an example of Python tuple iterator with any() method.

# app.py

tupA = ('Millie Bobby Brown', 'Finn Wolfhard', 'Noah Schnapp')
print(any(tupA))

tupB = ()
print(any(tupB))

tupC = (0, False)
print(any(tupC))

tupD = (1, False)
print(any(tupD))

tupE = ('0', 'False')
print(any(tupE))

See the following output.

➜  pyt python3 app.py
True
False
False
True
True
➜  pyt

So, it returns True if any of the elements is True. It returns False if tuple is empty or all are False or 0 values.

Any() method can be thought of as a sequence of OR operations on the provided iterables.

We can roughly think of any() and all() as a series of logical or and and operators, respectively.

#Truth table

+-----------------------------------------+---------+---------+
|                                         |   any   |   all   |
+-----------------------------------------+---------+---------+
| All Truthy values                       |  True   |  True   |
+-----------------------------------------+---------+---------+
| All Falsy values                        |  False  |  False  |
+-----------------------------------------+---------+---------+
| One Truthy value (all others are Falsy) |  True   |  False  |
+-----------------------------------------+---------+---------+
| One Falsy value (all others are Truthy) |  True   |  False  |
+-----------------------------------------+---------+---------+
| Empty Iterable                          |  False  |  True   |
+-----------------------------------------+---------+---------+

One important thing to know about any() and all() method is, it will short-circuit entire execution, the moment they know the output. The advantage is whole iterable need not be consumed.

Here’s the Python implementation of any() and all() are following.

def any(iterable):
    for i in iterable:
        if i:
            return True
    return False # for an empty iterable, any returns False!

def all(iterable):
    for i in iterable:
        if not i:
            return False
    return True  # for an empty iterable, all returns True!

Finally, Python any() Function Example | any() Method Tutorial is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.