In the previous article, we have discussed Python Program for ascii() Function
any() Function in Python:
The function any() returns True if any of the items in an iterable are true; otherwise, False.
The any() function returns False if the iterable object is empty.
Syntax:
any(iterable)
Parameters
iterable: It may be any iterable object like list, tuple, dictionary, etc.
Note: When applied to a dictionary, the any() function determines whether any of the keys are true, rather than the values.
Examples:
Example1:
Input:
Given list = [] Given tuple = (1, 2, 0, 0) Given dictionary = {1: 'hello', 2: 'this is', 0: 'btechgeeks'}
Output:
The result after applying any() function on the given list = False The result after applying any() function on the given tuple = True The result after applying any() function on the given dictionary = True
Example2:
Input:
Given list = [0, 1, 0, 1] Given tuple = (0, 0, 0) Given dictionary = {0: 'good', 0: 'morning'}
Output:
The result after applying any() function on the given list = True The result after applying any() function on the given tuple = False The result after applying any() function on the given dictionary = False
Program for any() Function in Python
Method #1: Using Built-in Functions (Static Input)
Approach:
- Give the list as static input and store it in a variable.
- Give the tuple as static input and store it in another variable.
- Give the dictionary as static input and store it in another variable.
- Apply any() function on the given list that returns True if any of the items in an iterable are true; otherwise, False.
- Store it in another variable.
- Apply any() function on the given tuple and store it in another variable.
- Apply any() function on the given dictionary and store it in another variable.
- Print the result after applying any() function on the given list.
- Print the result after applying any() function on the given tuple.
- Print the result after applying any() function on the given dictionary.
- The Exit of the Program.
Below is the implementation:
# Give the list as static input and store it in a variable. gvn_lst = [] # Give the tuple as static input and store it in another variable. gvn_tupl = (1, 2, 0, 0) # Give the dictionary as static input and store it in another variable. gvn_dictnry = {1: 'hello', 2: 'this is', 0: 'btechgeeks'} # Apply any() function on the given list that returns True if any of the items # in an iterable are true; otherwise, False. # Store it in another variable. rslt_lst = any(gvn_lst) # Apply any() function on the given tuple and store it in another variable. rslt_tupl = any(gvn_tupl) # Apply any() function on the given dictionary and store it in another variable. rslt_dictnry = any(gvn_dictnry) # Print the result after applying any() function on the given list. print("The result after applying any() function on the given list = ", rslt_lst) # Print the result after applying any() function on the given tuple. print("The result after applying any() function on the given tuple = ", rslt_tupl) # Print the result after applying any() function on the given dictionary. print("The result after applying any() function on the given dictionary = ", rslt_dictnry)
Output:
The result after applying any() function on the given list = False The result after applying any() function on the given tuple = True The result after applying any() function on the given dictionary = True
Method #2: Using Built-in Functions (User Input)
Approach:
- Give the list as user input using list(),map(),input(),and split() functions and store it in a variable.
- Give the tuple user input using list(),map(),input(),and split() functions and store it in another variable.
- Take a dictionary and initialize it with an empty dictionary using dict() or {}.
- Give the number of keys as user input using int(input()) and store it in a variable.
- Loop till the given number of keys using for loop.
- Inside the for loop scan the key and value as user input using input(), split() functions, and store them in two separate variables.
- Initialize the key with the value of the dictionary.
- Apply any() function on the given list that returns True if any of the items in an iterable are true; otherwise, False.
- Store it in another variable.
- Apply any() function on the given tuple and store it in another variable.
- Apply any() function on the given dictionary and store it in another variable.
- Print the result after applying any() function on the given list.
- Print the result after applying any() function on the given tuple.
- Print the result after applying any() function on the given dictionary.
- The Exit of the Program.
Below is the implementation:
# Give the list as User input using list(),map(),input(),and split() functions # and store it in a variable. gvn_lst = list(map(int, input( 'Enter some random List Elements separated by spaces = ').split())) # Give the tuple as User input using list(),map(),input(),and split() functions # and store it in another variable. gvn_tupl = tuple(map(int, input( 'Enter some random Tuple Elements separated by spaces = ').split())) # Take a dictionary and initialize it with an empty dictionary using dict() or {}. gvn_dict = {} # Give the number of keys as user input using int(input()) and store it in a variable. numb_of_kys = int( input('Enter some random number of keys of the dictionary = ')) # Loop till the given number of keys using for loop. for p in range(numb_of_kys): # Inside the for loop scan the key and value as # user input using input(),split() functions # and store them in two separate variables. keyy, valuee = input( 'Enter key and value separated by spaces = ').split() # Initialize the key with the value of the dictionary. gvn_dict[int(keyy)] = valuee # Apply any() function on the given list that returns True if any of the items # in an iterable are true; otherwise, False. # Store it in another variable. rslt_lst = any(gvn_lst) # Apply any() function on the given tuple and store it in another variable. rslt_tupl = any(gvn_tupl) # Apply any() function on the given dictionary and store it in another variable. rslt_dictnry = any(gvn_dict) # Print the result after applying any() function on the given list. print("The result after applying any() function on the given list = ", rslt_lst) # Print the result after applying any() function on the given tuple. print("The result after applying any() function on the given tuple = ", rslt_tupl) # Print the result after applying any() function on the given dictionary. print("The result after applying any() function on the given dictionary = ", rslt_dictnry)
Output:
Enter some random List Elements separated by spaces = 0 1 0 1 Enter some random Tuple Elements separated by spaces = 0 0 0 Enter some random number of keys of the dictionary = 2 Enter key and value separated by spaces = 0 good Enter key and value separated by spaces = 0 value The result after applying any() function on the given list = True The result after applying any() function on the given tuple = False The result after applying any() function on the given dictionary = False
Find a Comprehensive Collection of Python Built in Functions that you need to be aware of and use them as a part of your program.