Python any()

A handy built-in function called Python any() helps you check if at least one item in a collection is considered True. When you use Python any(), you give it a sequence of items like a list, tuple, or string, and it goes through each item individually. If it finds even a single item that evaluates to True, the function immediately returns True.
Table of Contents

Understanding Python any() Function

Python any() is a built-in function that helps you check if at least one element exists in a collection of items. Think of it like asking a yes/no question about your collection. It checks each item in a collection (like a list, tuple, or string) and returns True if it finds even one item; otherwise, it will return False. If the collection is empty, any() will always return False. It’s a simple way to determine if something you are looking for exists within a group of items.

Syntax of Python any()

result = any(iterable)

Explanation

  • result: Variable will store the result, either True or False.
  • any(): Function that checks if at least one element is True.
  • iterable: Collection of items (e.g., a list, tuple, string) that you want to check. This is the input of any() function.

Example of Python any()

my_list = [False, False, True, False]
result = any(my_list)
print(result)

Explanation

  • my_list = [False, False, True, False]: Creates a list named my_list containing boolean values.
  • result = any(my_list): The any() function checks if at least one element in my_list is True and stores in result.
  • print(result): Prints the value of result, which will be True because there is one True in the list.

Output

True


any() Parameters

The any() function in Python takes a single parameter: an iterable. This iterable can be a list, a tuple, a set, a dictionary (in which case it checks the keys), or even a custom iterable object. Python any() examines each element within this iterable to determine its truthiness. The function stops and returns True when encountering any element that evaluates True.

Syntax

boolean_result = any(iterable_object)

Explanation

  • boolean_result: This stores returned boolean value.
  • any(): This is the function call.
  • iterable_object: List, tuple, set, dictionary, or a custom iterable.

Example

my_tuple = (0, 0.0, "", "hello")  # Empty string is False, "hello" is True
has_true_value = any(my_tuple)
print(has_true_value)

Explanation

  • my_tuple = (0, 0.0, "", "hello"): Creates a tuple with various values.
  • has_true_value = any(my_tuple): Checks if any element in my_tuple is truthy.
  • print(has_true_value): Prints returned value.

Output

True


any() Return Value

Python any() function provides a boolean value: True or False. It returns True if at least one element within the iterable you provided is “truthy”. If all elements in the iterable are “falsy,” or if the iterable is empty, then any() returns False. It efficiently “short-circuits,” meaning it stops checking when it finds a True value.

Syntax

true_or_false = any(some_iterable)

Explanation

  • true_or_false: Will store the boolean result.
  • any(): Checks for any truthy element.
  • some_iterable: Input iterable (list, tuple, etc.).

Example

empty_list = []
result = any(empty_list)
print(result)

Explanation

  • empty_list = []: Creates an empty list.
  • result = any(empty_list): Calls any() on the empty list.
  • print(result): Prints False because the list is empty.

Output

False


Basic List Check with Python any()

When you use python any with a list, the function checks each element of the list. If it encounters at least one element considered True, the function immediately returns True. It doesn’t matter if there are other True elements later in the list. The elements can be boolean values or values of other data types. However, if all elements in the list are considered False, or if the list is empty, any() returns False.

Syntax

result = any(list)

Explanation

  • result: Variable will store the boolean result, True or False.
  • any(): Function that checks the elements in the list.
  • list: List you want to check. It is the input for any() function.

Example

my_list = [0, 0, 1, 0]
result = any(my_list)
print(result)

Explanation

  • my_list = [0, 0, 1, 0]: Creates a list of integers named my_list. Here 0 is considered as False and 1 is considered as True.
  • result = any(my_list): The any() function checks the list, finds that at least one element (1) is True and stores it in result.
  • print(result): Line prints the value of result, which is True.

Output

True


String Check with Python any()

Using any in Python with a string checks each character in the string. An empty string evaluates to False, while any non-empty string is considered True. So, any() function will return True as long as the string has at least one character; otherwise, it will return False.

Syntax

result = any(string)

Explanation

  • result: Variable will store the boolean result.
  • any(): Function that checks the characters in the string.
  • string: String you’re evaluating. It is the input for any() function.

Example

my_string = "hello"
result = any(my_string)
print(result)

Explanation

  • my_string = "hello": Creates a string named my_string.
  • result = any(my_string): The any() function checks the string. Since it’s not empty, it returns True and stores in result.
  • print(result): Prints the value of result, which is True.

Output

True


Dictionary Check (Values) with Python any()

When you use Python any() with a dictionary, it checks the values of the dictionary, not the keys. It will iterate through each value in the dictionary, and if it finds that at least one value is considered True, any() will return True. Otherwise, it will return False. An empty dictionary will always result in False.

Syntax

result = any(dictionary.values())

Explanation

  • result: Variable stores the boolean result.
  • any(): Function checks if any of the dictionary values are True.
  • dictionary.values(): This provides a view of all the values in the dictionary. It is passed as input to any().

Example

my_dict = {'a': 0, 'b': 0, 'c': 5}
result = any(my_dict.values())
print(result)

Explanation

  • my_dict = {'a': 0, 'b': 0, 'c': 5}: Creates a dictionary named my_dict with integer values.
  • result = any(my_dict.values()): The any() function checks the dictionary values. It finds that at least one value (5) is True and stores in result.
  • print(result): Prints the value of result, which is True.

Output

True


List with Condition with Python any()

You can use Python any along with conditions to check if at least one element in a list satisfies a particular condition. This is generally done using a generator expression inside the any() function. The generator expression evaluates the condition for each element, and any() returns True if the condition is met for at least one element. Otherwise, it will return False.

Syntax

result = any(condition for item in list)

Explanation

  • result: Variable will store the boolean result.
  • any(): Function checks if the condition is True for any item.
  • condition for item in list: Generator expression that applies the condition to each item in the list. This acts as an input for any().

Example

my_list = [1, 2, 3, 4, 5]
result = any(x > 3 for x in my_list)
print(result)

Explanation

  • my_list = [1, 2, 3, 4, 5]: Creates a list of integers named my_list.
  • result = any(x > 3 for x in my_list): The any() function, along with the generator expression checks if any number in my_list is greater than 3 and stores in result.
  • print(result): Prints the value of result, which is True because there are numbers greater than 3 in the list.

Output

True


Conclusion

Python any() function is useful for checking item collections in your code. Whether you’re working with lists, strings, or dictionaries, any() provides a concise way to determine if at least one element meets a specific condition or is considered True. Understanding how any in python works can simplify your code and make it more readable, especially when dealing with logical checks on collections. Remember it will always return a boolean value True or False.


Also Read

Python abs()

Python all()


Python Reference

python any()