Python

Python any() function usage

any() is a built-in function of python to check the items of multiple data type objects like tuple or list or dictionary and any item contains true then the function will return true.  For example, if at least one item of a tuple contains the true value and the tuple is passed as an argument of any() function then the method will return true. But if all items of the tuple contain false value then the return value of any() function will be false. This function works like logical OR conditions that return true if any one condition is true.  The usage of any() function in python is described in this tutorial.

Syntax:

any(iterable_variable)

Here, iterable_variable can be any tuple or list or any iterable object and it returns a Boolean value.   The uses of any() function on different iterable objects are shown below.

Use of any() function on the string

Any string value is considered as the true value for any() function. In the following example, string data is stored in the variable, text1 and when the variable is passed as an argument for any() function then it returns true. When an empty string is stored in the variable, text2, and passed to any() function then it returns false because the empty string is considered as false.

#!/usr/bin/env python3

# Apply any() on string data
text1 = "Linux Hint"
print("The output of string value:", any(text1))

# Apply any() on empty data
text2 = ""
print("The output of empty string value:", any(text2))

Output:

The following output will appear after running the script.

Use of any() function on tuple

The following script shows the usage of any() function on different types of tuple variables. tup1 contains all numeric values and all returns true except one. tup2 contains four false values and one negative value (-1) that returns true. tup3 contains two false values and two empty values that return false also. tup4 contains two false values, one string value that returns true and one empty string that returns false.

#!/usr/bin/env python3

# Apply any() on numeric data
tup1 = (15, 23, 43, 0, 78)
print("The first output:", any(tup1))

# Apply any() on boolean data and negetive number
tup2 = (0, False, False, -1, False)
print("The second output:", any(tup2))

# Apply any() on boolean data and empty string
tup3 = ('', False, '', False)
print("The third output:", any(tup3))

# Apply any() on boolean data and string value
tup4 = ('Hello', False, '', False)
print("The fourth output:", any(tup4))

Output:

The following output will appear after running the script.

Use of any() function on the list

The following script shows the usage of any() function on list variables. The four types of list variables are used here. list1 is an empty list that returns false. list2 contains three string values that return true and an empty value that returns false. list3 contains two zero numbers (0) that return false and a character, ‘0’ that returns true.  list4 contains three values, one zero that returns false, one false and one empty string that returns zero. So, all values of list4 are false.

#!/usr/bin/env python3

# Apply any() on an empty list
list1 = []
print("The output of empty list:" ,any(list1))

# Apply any() on a list of string
list2 = ['Ubuntu', '', '0', 'Fedora']
print("The output of a list of string:" ,any(list2))

# Apply any() on a list of zero values
list3 = [0, '0', 0]
print("The output of a list of 0 values:" ,any(list3))

# Apply any() on a list of boolean and empty string
list4 = [0, False, '']
print("The output of a list of boolean and empty data:" ,any(list4))

Output:

The following output will appear after running the script.

Use of any() function on dictionary

The following script shows the usage of any() function on the dictionary variable. any() function returns value based on the index values of the dictionary. any() function is applied to three dictionary variables here. dic1 contains only one item where the index is 0 that returns false. dic2 contains two items, the index of the first item is 0 that returns false and the index of the second item is a string value that returns true. dic3 contains two items, the index of the first item is false and the index of the second item is an empty string that returns false also.

#!/usr/bin/env python3

# Apply any() on a dictionary of single item where index is 0
dic1 = {0:'True'}
print("The first output: ", any(dic1))

# Apply any() on a dictionary of two items where indexes are 0 and 'False'
dic2 = {0: 'False', 'False': 0}
print("The second output: ", any(dic2))

# Apply any() on a dictionary of two items where indexes are False and empty string
dic3 = {False: False, '': 'Empty'}
print("The third output: ", any(dic3))

Output:

The following output will appear after running the script.

Use of any() function on multiple conditions

In the following example, any() function is applied on three list variables, and the outputs are used in an if statement with logical AND operators. The first list contains one true value(-1) and it returns true. The second list contains two true values( ‘False’, ‘0’) and it returns true. The third list contains all false values that return false. So, if condition will return false.

#!/usr/bin/env python3

# Apply any() on first list
List1 = [0, -1, False]
print("List1 : " ,any(List1))

# Apply any() on second list
List2 = ['','False', '0']
print("List2 : " ,any(List2))

# Apply any() on third list
List3 = [False, 0, '']
print("List3 : " ,any(List3))

# Returns true if all the outputs of any() functions are True
if(any(List1) and any(List2) and any(List3)):
  print("The output of all any() function are True")
else:
  print("Any one output of any() function is False")

Output:

The following output will appear after running the script.

Conclusion:

The usage of any() function on different types of variables in python is shown using various examples here. It will help the readers to understand the use of any() function in python and apply it properly in the script.

About the author

Fahmida Yesmin

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.