The any() function in Python is a built-in function that returns True value if any element in an iterable object is True. It plays a critical role in many programming applications.
Python any() function iterates over elements of the iterable object (such as string, list, tuple, etc.) and checks whether any of them are True.
If this function encounters the first element as True, without checking the remaining elements, it returns True value. Otherwise, the function returns False if all elements are False.
Syntax of any() Function in Python
The general syntax of the any() function in Python is as follows:
any(iterable)
Parameters:
This function accepts only one parameter “iterable” object that represents any sequence or collection of elements, such as a string, list, tuple, set, or dictionary object.
Return type:
This method returns a boolean value:
- True: if any element in the iterable object is true.
- False: if all the elements in the iterable object are false.
If the iterable object is empty or falsy value, such as 0, False, or none, all() function returns False.
Example Program based on any() Function
Here are some important example programs that demonstrate the usage of the Python any() function in different contexts.
Example 1: Using any() method with a list
# Creating a list of elements. my_list = [False, True, False, 0] result = any(my_list) print(result
Output: True
In this example, we have created a list of elements. Then, we have used any() function that iterates over each element in the list. The any() function checks each element of the list and returns True because the second element is True.
Example 2:
# Creating five lists of different elements. empty_list = [] print(any(empty_list)) # returns False my_list = [0] print(any(my_list)) # returns False my_list = [0, 1, 2, 3, 4, 5] print(any(my_list)) # returns True my_list = [0, False] print(any(my_list)) # returns False my_list = [0, 1, False] print(any(my_list)) # returns True
Output: False False True False True
Example 3: Using any() function with a string
We know that a string is also an iterable type object in Python. The any() function returns False boolean value for an empty string.
# Creating a string. str = "Scientech Easy" result = any(str) print(result) # Creating an empty string. str = "" result = any(str) print(result)
Output: True False
Example 4: Using any() function with a dictionary
# Creating two dictionaries with different key-value pairs. my_dict = {0: 'zero', 1: 'One', 2: 'two'} result = any(my_dict) print(result) my_dict = {0: 'zero'} result = any(my_dict) print(result)
Output: True False
Usage of any() Function in Practical Applications
The Python any() function has many practical applications in programming. For example, we can use any() function to check if any value in a list meets a specific condition or to validate user input in a program.
Here is an example code that uses the Python any() function to check if any value in a list is greater than 5.
Example 5:
# Create a list of four numeric elements my_list = [1, 2, 4, 5, 8] result = any(x > 5 for x in my_list) print(result)
Output: True
In this example, the any() function returns True because the fifth element 8 in the list is greater than 5.
Example 6:
# Python program to validate user input for a yes or no. user_input = input("Do you want to proceed? (y/n): ") result = any(answer.lower() == "y" for answer in user_input) if result: print("Proceeding...") else: print("Exiting program.")
Output: Do you want to proceed? (y/n): Y Proceeding...
In this example, we have prompted the user to input a yes or no response. Then, we have called any() function to check if the user’s response contains the letter “y”.
Here, we have used the lower() method to convert the response to lowercase before checking. If the response from the user contains “y”, the program proceeds; otherwise, it exits.
Example 7:
# Python program to check if any string in a list contains a particular substring. my_list = ["apple", "banana", "orange", "kiwi", "mango"] substring = "man" result = any(substring in string for string in my_list) print(result)
Output: True
In this example code, we have created a list of strings and a substring as another string. Then, we have used a generator expression inside any() function to check if any of the strings in the list contain the substring. Since the substring “man” appears in “mango”, the function returns True.
Example 8:
# Python program to check if any number in a list is even. my_list = [3, 5, 8, 11, 12] result = any(num % 2 == 0 for num in my_list) print(result)
Output: True
In this example code, we have defined a list of numbers and used a generator expression inside any() function to check if any of the numbers are even. Since the numbers 8 and 12 are even, the function returns True.
Google Search FAQs
Q. Can we use any() function with a string in Python?
Yes, we can use any() function with any iterable object, including string.
Q. What happens we pass an empty iterable object to the any() function?
If we pass an empty iterable object to the any() function, it will return False.
Q. How does any() function differ from all() function in Python?
The Python any() function returns True if any element in the iterable object is True, while the all() function returns True only if all elements in the iterable object are True.
Q. Can we use any() function with custom objects?
Yes, we can use any() function with any iterable object, including custom objects.
Q. What is the time complexity of the any() function in Python?
The time complexity of the any() function in Python is O(n), where n is the number of elements in the iterable object.
In this tutorial, you have learned how to use any() function in Python programming. Hope that you will have understood the basic usage of any() function in practical applications and practiced all example programs.
Thanks for reading!!!