Methods and Functions

Python any()

Python any() method is a built-in function that returns TRUE if any of the items of a provided iterable (List, Dictionary, Tuple, Set, etc.) are true; otherwise, it returns FALSE. The Python any() function returns the value TRUE if any item in an iterable is true; else it returns FALSE.

As a Python coder, you’ll constantly encounter Booleans and conditional expressions, which can be quite complex. In such cases, you should rely on a function that simplifies the reasoning. Fortunately, Python any() method provides these capabilities. It iterates through an iterable and returns a single result indicating if any element is true in a Boolean context. Python any() is a built-in function that is used for sequential And/Or.

Python any() function

Python any Syntax

The syntax for Python any() is as follows:

Parameters for Python any function

Python all() function accepts a single parameter:

iterable = can be an iterable object such as a list, tuple, dictionary, set, etc.

Return value from any()

The any() function returns a Boolean value:

TRUE if at least one of the elements in the iterable is true

FALSE if all the elements are false or if the iterable is empty

Condition Return Value
All values are True TRUE
All values are False FALSE
At least one value is True (others are False) TRUE
At least one value is False (others are True) TRUE
Iterable is Empty FALSE

All False values in Python

All of the following values are considered as False in Python:

  • Constants that are defined as false: FALSE and NONE.
  • A zero of any form of numeric value: Decimal(0), Fraction(0, 1), 0, 0.0, 0j
  • “, (), [], {}, set(), range(0) are examples of empty sequences and collections

Example 1: Using python List any

If any of the elements in the list passed as an argument is true, then Python any() function returns a TRUE value. To demonstrate the result of the any() function in various situations, we shall consider numerous lists with different entries.

Note – The Python any() function works similarly for tuples and sets just like the lists.

Example

                    

# Python program to illustrate any()
# all values are True
list1 = [1, 2, 3, 4]
print(any(list1))

# all values are False
list2 = [0, False]
print(any(list2))

# True since 2, 4 are True
list3 = [0, 2, 4]
print(any(list3))

# False since the list is empty
list4 = []
print(any(list4))

Output

                    

True
False
True
False

Explanation

In the first list, all the values are True so the any() function returns TRUE as its output. In the second list, both 0 and False are considered as False in Python. Hence the returned value is FALSE. In the third case, there exists 1 False value and 2 True values. According to any(), the returned value for such a situation is TRUE. In the final case, the list is kept empty which contains a False value. So, Python any() returns FALSE.

Example 2: Using any() on Python Strings

Any character, number, or a text inside the quotation mark is considered as a TRUE value. Only if the string is empty, then the any() function returns a FALSE value.

Example

                    

# Python program to illustrate any()
# all values are True
txt1 = 'Hello World'
print(any(txt1))

# '0' is considered as a True string
txt2 = '0'
print(any(txt2))

# empty string iterable
txt3 = ''
print(any(txt3))

Output

                    

True
True
False

Example 3: Using any() with Python Dictionaries

When you use the any() method on a dictionary, it just checks the keys, not the values. If any of the keys in the dictionary is true, then any() function returns TRUE; otherwise, it returns FALSE. The any() function also returns FALSE when the dictionary is empty.

Example

                    

# key 0 and False are False
dict1 = {0: 'False', False: 0}
print(any(dict1))

# all keys are True
dict2 = {1: 'John', 2: 'David', 3: 'Kim'}
print(any(dict2))

# few keys are True
dict3 = {0: 'False', 1: 'True'}
print(any(dict3))

# empty dict
dict4 = {}
print(any(dict4))

# a key of string is True
dict5 = {'0': False, 'False': 1}
print(any(dict5))

Output

                    

False
True
True
False
True

FAQs on Python any

Q1. What is any() in Python?

Python any(), an inbuilt function is used to iterate through an iterable/object and return a single result indicating if any element is true in a Boolean context. Python any() is a built-in function that is used for sequential And/Or. The Python any() function returns the value TRUE if any item in an iterable is true; else it returns FALSE.

Q2. How does any() work?

Python any() method is a built-in function that returns true if any of the items of a provided iterable (List, Dictionary, Tuple, Set, etc.) are TRUE; otherwise, it returns FALSE. The syntax for Python any() accepts a maximum of one single parameter.

The any() function works on the following principle:

It returns a Boolean value:

  • TRUE if at least one of the elements in the iterable is true
  • FALSE if all the elements are false or if the iterable is empty

Example

                    

list1 = [0, 1, 2]
print(any(list1))

list2 = [0, False]
print(any(list2))

dict1 = {0: False, 1: True}
print(any(dict1))

txt1 = 'Programming'
print(any(txt1))

txt2 = ''
print(any(txt2))

Output

                    

True
False
True
True
False

Q3. How do you use any() or all() in Python?

Python has built-in functions such as any() and all(). They are both convenient functions that help to reduce code length by replacing loops.

  1. all() – Python all() function returns TRUE only if all the iterables passed to the function are True, else it returns FALSE.
  2. any() – Python any() function returns TRUE if at least one of the elements in the iterable is True, else it returns FALSE.
all() Return value any() Return value
All values are True TRUE TRUE
All values are False FALSE FALSE
At least one value is True (Others are False) FALSE TRUE
At least one value is False (Others are True) FALSE TRUE
Empty Iterable TRUE FALSE

Example

                    

# Python program to illustrate all() and any()
list1 = [0, 1, 2, 3]
print(any(list1))
print(all(list1))
print('')

list2 = ['Hello', 'Hi']
print(any(list2))
print(all(list2))
print('')

dict1 = {0: 'False', False: 0}
print(any(dict1))
print(all(dict1))
print('')

txt = ''
print(any(txt))
print(all(txt))

Output

                    

True
False

True
True

False
False

False
True

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.