BlogsDope image BlogsDope

Python any() function

Jan. 23, 2021 PYTHON FUNCTION 707

The Python interpreter has a number of utility functions and types built into it that are always available and help in performing various operations easily. One such utility function is the Python any() function that returns a boolean value.

The any() function accepts an iterable such as a list, tuple, dictionary etc. as an argument and then returns True if any of the element in iterable is true, otherwise it returns False. If iterable passed is empty, then any() method returns False.

Python any() Syntax:

The following is the syntax of the any() function:

​any(iterable)


Parameters:

The any() function takes python iterables as its arguments such as:

  • Lists, e.g. [1,2,False]
  • Tuples, e.g. (5,6,7)
  • Dictionaries, e.g. {1:"John", 2:"Mark"}
  • Sets, e.g. {55,63,14}
  • Strings, e.g. "Miachel", "Trevor".

*Note that the values in the iterables might not necessarily be all boolean values. 

Working and Return value:

The any() function is an equivalent to a series of successive logical or operations on the provided iterable. So just like the or operation if any value in the iterable is true it returns True else it returns False.

Iterable values
any() method return value
All Truth values
True
All False values
False
One Truth value and all others False
True
One False value and all others True
True
Empty iterable
False

Actually, the any() method is a utility method and it is a compact version of the following code:

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

Examples:

any() function with Lists:

# all true values
ls = [1,5,6,7]
print(any(ls))

# all false values
ls = [0, False]
print(any(ls))

# one true value
ls = [0, False, 8]
print(any(ls))

# one false value
ls = [1, False, 9]
print(any(ls))

# empty list
ls = []
print(any(ls))

Output:

True
False
True
True
False

any() function with Tuples:

# all true values
tup1 = (1,'Codesdope',6)
print(any(tup1))

# all false values
tup2 = (False, 0, False)
print(any(tup2))

# one true value
tup3 = (0, False, 3)
print(any(tup3))

# one false value
tup4 = [1, False, 4]
print(any(tup4))

# empty tuple
tup5 = ()
print(any(tup5))

Output:

True
False
True
True
False

any() function with Dictionaries:

The any() function checks for the truth values of the keys in a dictionary. So if at least one key is true it returns True.
# all true values
D = {1:"Cat",2:'Dog'}
print(any(D))

# all false values
# 0 denotes false
D = {False: 0, 0:"False"}
print(any(D))

# one true value
# 1 denotes true
D = {1:"True", False:"Fish", 0:"Frog"}
print(any(D))

# one false value
D = {1:"One", 0:"Zero", 4:"Four"}
print(any(D))

# empty list
D = {}
print(any(D))

Output:

​True
False
True
True
False

any() function with Strings:

# all true values
st = "Python is Great"
print(any(st))

# False is boolean value
# 'False' is string so method return true
st = 'False'
print(any(st))

# 0 is numeric value hence it gives false
# '0' is a string character so method return true
st = '00000'
print(any(st))


# empty string
st = ''
print(any(st))

Output:

​True
True
True
False

When to use any() function?

The compact syntax of any() function makes it very easy to execute and it can be used when we want to apply or function successively. Instead of applying logical or on every member of the iterable using a loop, we can just pass the iterable into any() function. Another advantage is that if a single value is found to be True in the iterable, the rest of the iterable need not to be scanned which reduces execution time.

That's all for the Python any() function.


Liked the post?
Zealous Learner.
Editor's Picks
0 COMMENT

Please login to view or add comment(s).