W3Basic Logo

Python any() Function

The any() method returns True if any element in the iterable is true, or if not, it returns False.

Example:

# Check if one of the elements is true
is_available = [True, "True", 1]
output = any(is_available)
print(output)

Output

True

Syntax

The Syntax of the any() function is:

any(iterable)

Here, iterable could be a list, tuple, set, dictionary, etc.

Parameters

The any() function takes single parameter.

  • iterable - the iterable, which contains a series of elements. The iterable could be of type list, tuple, dictionary, etc.

Return Value

The any() function returns:

  • Returns True if at least one of the elements in the iterable is true
  • Returns False if at least all the elements in the iterable are false
  • Returns False if the iterable is empty
ConditionReturn Value
All the elements are trueTrue
All the elements are falseFalse
One of the elements is true, and others are false)True
One of the elements is false, and others are trueTrue
Empty IterableFalse

Difference between all() and any() Methods

The all() and any() methods are similar to the Logical AND and OR operators in Python.

The all() method returns True when at least all the elements in the iterable are true, whereas the any() method returns True when at least one of the elements in the iterable is true.

Scenarioany()all()
All Truthy valuesTrueTrue
All Falsy valuesFalseFalse
One Truthy value (all others are Falsy)TrueFalse
One Falsy value (all others are Truthy)TrueFalse
Empty IterableFalseTrue

Example 1: Using any() method with list in Python

The any() method in the below example returns True if at least one of the elements in the iterable is true.

# All the elements in the list are true
lst = [1, 2, 3, 4, 5]
print(any(lst))

# All the elements in the list are false
lst = [0, 0, False]
print(any(lst))

# One of the elements in the list is false
lst = [1, 2, 3, 4, False]
print(any(lst))


# Only one element in the list is true
lst = [0, 0, "False", False, 100]
print(any(lst))

# Empty List
lst = []
print(any(lst))

Output

True
True
True
True
False

Example 2: Using any() method with Dictionary

In the case of dictionaries, the any() method returns True if at least one of the keys in the dictionary is true.

Note: It only considers the dictionary keys and not the values.

# All the elements in the dictionary are true
item = {1: "Python", 2: "Java", 3: "C#"}
print(any(item))

# All the elements in the dictionary are false
item = {0: "Python", False: "C#"}
print(any(item))

# One of the elements in the dictionary is false
item = {0: "Python", 1: "Java", 2: "C#"}
print(any(item))

# Empty dictionary
item = {}
print(any(item))

Output

True
False
True
False

Example 3: Using any() method with Tuple

The any() method in the below example returns True if at least one of the elements in the tuple is true.

# All the elements in the tuple are true
item = (1, 2, 3, 4, 5)
print(any(item))

# All the elements in the tuple are false
item = (0, 0, False)
print(any(item))

# One of the elements in the tuple is false
item = (1, 2, 3, 4, False)
print(any(item))

# Only one element in the tuple is true
item = (0, 0, "False", False, 100)
print(any(item))

# Empty tuple
item = ()
print(any(item))

Output

True
False
True
True
False

Example 4: Using any() method with Strings

In the case of a string, even if the string value is "0", the any() method returns True since it's a valid string value and evaluates to Truthy.

# Non-Empty String
text = "Welcome to Python Tutorial"
print(any(text))

# Empty String
text = ""
print(any(text))

# Text with 0 is true as its string
text ="0"
print(any(text))

Output

True
False
True

Reference: Python Official Docs

© 2023 W3Basic. All rights reserved.

Follow Us: