In this section we will learn what the any() function is and how to use it in Python.
What is Python any() Function?
The Python any() function is used to check the items of an iterable object and see if only one item of that iterable object is True or interpret as True!
For numbers, they are True except the number 0. For other values; they are also True for as long as they are not empty.
For example, an empty string, empty array, empty dictionary, empty set etc. these are Falsy values.
Python any() Function Syntax:
any(iterable)
Python any() Function Parameters
The function takes one argument and that is a reference to an iterable object that we want to check its items.
Python any() Function Return Value
The return value of this method will be True if only one item in an iterable object interprets as True.
Otherwise, if entire elements of the target iterable object interpreted as False, the final result of this function will be False as well.
Example: any() function and dictionary
dictionary = { "name":"John", "lastName": "Doe", "age": 200, } result = any(dictionary) print(result);
Output:
True
Note that the any() function will check the keys of a dictionary to see if they interpret as False.
Python any() Function and Dictionary Example:
dic = { False: "Jack" } print(any(dic))
Output:
False
Example: any() function and lists
list1 = [1,2,3,4,5,6,8,0] result = any(list1) print(result);
Output:
True
Example: any() function and string
list1 = ["","1"] print(any(list1)) print(any("")) print(any("0")) print(any(" ")) print(any("John Doe"))
Output:
True False True True True