Python any()

The any() method returns True if any element of an iterable is true. If not, this method returns False.

The syntax of any() is:

any(iterable)

any() Parameters

They any() method takes an iterable (list, string, dictionary etc.) in Python.


Return Value from any()

The any method returns:

  • True if at least one element of an iterable is true
  • False if all elements are false or if an iterable is empty
When Return Value
All values are true True
All values are false False
One value is true (others are false) True
One value is false (others are true) True
Empty Iterable False

Example 1: How any() works with Python List?

l = [1, 3, 4, 0]
print(any(l))

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

l = [0, False, 5]
print(any(l))

l = []
print(any(l))

When you run the program, the output will be:

True
False
True
False

The any() method works in similar way for tuples and sets like lists.


Example 2: How any() works with Python Strings?

s = "This is good"
print(any(s))

# 0 is False
# '0' is True
s = '000'
print(any(s))

s = ''
print(any(s))

When you run the program, the output will be:

True
True
False

Example 3: How any() works with Python Dictionaries?

In case of dictionaries, if all keys (not values) are false, any() returns False. If at least one key is true, any() returns True.

d = {0: 'False'}
print(any(d))

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

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

d = {}
print(any(d))

# 0 is False
# '0' is True
d = {'0': 'False'}
print(any(d))

When you run the program, the output will be:

False
True
False
False
True