Home » Python

any() function with example in Python

Python any() function: Here, we are going to learn about the any() function in Python with example.
Submitted by Anshuman Singh, on June 15, 2019

Python any() function

any() function is used to check whether any of the elements of an iterable object is True or not. It accepts an iterable object and returns True, if one or more than one elements are true, else it returns False.

If any item in iterable object (eg. list, tuple and dictionary) is true then any() function returns True otherwise False.

If iterable object is empty ( eg. list = [], tuple=() ) then any() function returns False.

Syntax:

    any(iterable_object)

Parameter: iterable_object – an iterable object like list, tuple, dictionary.

Return value: bool – a Boolean value

1) Program to describe any() function with list in python

if __name__ == "__main__":
    # any() function will return True because 
    # 1st item is true in list_1
    list_1 = [1, 2, 0, 3]
    result = any(list_1)
    print ("list_1: {}".format(result))
    
    # any() function will return False because 
    # none of the items are true in list_2
    list_2 = [0, False, '', 0.0, [], {}, None]
    result = any(list_2)
    print ("list_2: {}".format(result))
    
    # any() function will return False because 
    # list_3 is empty
    list_3 = []
    result = any(list_3)
    print ("list_3: {}".format(result))

Output

list_1: True
list_2: False
list_3: False

2) Program to describe any() function with string in python

if __name__ == "__main__":
    # any() function will return True because 
    # string_1 is not none
    string_1 = "Hello! python"
    print ("string_1: {}".format(any(string_1)))
    
    # any() function will return True because 
    # string_2 is not none. Here, '0' is True and 0 is false
    string_2 = "0"
    print ("string_2: {}".format(any(string_2)))
    
    # any() function will return False because 
    # string_3 is none
    string_3 = ""
    print ("string_3: {}".format(any(string_3)))

Output

string_1: True
string_2: True
string_3: False

3) Program to describe any() function with dictionaries in python

In case of dictionaries, If all keys are false, any() returns False. If atleast one key is true, any() returns True.

if __name__ == "__main__":
    # any() function will return False because 
    # key in dictonary_1 is False.
    dictonary_1 = {0: 'False'}
    print("dictonary_1: {}".format(any(dictonary_1)))
    
    # any() function will return True because 
    # second key(i.e 1) in dictonary_2 is True.
    dictonary_2 = {0: 'False', 1: 'True'}
    print("dictonary_2: {}".format(any(dictonary_2)))
    
    # any() function will return False because 
    # all keys in dictonary_3 is False.
    dictonary_3 = {0: 'False', False: 0}
    print("dictonary_3: {}".format(any(dictonary_3)))
    
    # any() function will return False because 
    # dictonary_4 is empty.
    dictonary_4 = {}
    print("dictonary_4: {}".format(any(dictonary_4)))
    
    # any() function will return True because 
    # key(i.e '0') in dictonary_5 is True.
    # 0 is False
    # '0' is True
    dictonary_5 = {'0': 'False'}
    print("dictonary_5: {}".format(any(dictonary_5)))

Output

dictonary_1: False
dictonary_2: True
dictonary_3: False
dictonary_4: False
dictonary_5: True

4) Program to describe any() function with tuple in python

if __name__ == "__main__":
    # any() function will return True because 
    # 1st item in tuple_1 is True.
    tuple_1 = (1, "BeginnersBook", 22)
    print("tuple_1: {}".format(any(tuple_1)))
    
    # any() function will return False because 
    # all items in tuple_2 is False.
    tuple_2 = (0, False, 0)
    print("tuple_2: {}".format(any(tuple_2)))
    
    # any() function will return True because 
    # 1st item in tuple_3 is True.
    tuple_3 = (1, 0, False)
    print("tuple_3: {}".format(any(tuple_3)))
    
    # any() function will return True because 
    # 2nd item in tuple_4 is True.
    tuple_4 = (False, 1, 2, 3)
    print("tuple_4: {}".format(any(tuple_4)))
    
    # any() function will return False because 
    # tuple_5 is empty.
    tuple_5 = ()
    print("tuple_5: {}".format(any(tuple_5)))   

Output

tuple_1: True
tuple_2: False
tuple_3: True
tuple_4: True
tuple_5: False





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL







Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.