PYTHON

Python any() Function: Syntax, Usage, and Examples

The any() function checks if at least one value in an iterable is truthy. It returns True as soon as it finds a truthy item, otherwise it returns False.


How to Use the any() function

You call the any() function with one argument, an iterable like a list, tuple, set, or generator.

Basic syntax

any(iterable)

  • If the iterable contains at least one truthy value, any() returns True
  • If all values are falsy, any() returns False
  • If the iterable is empty, any() returns False

Here’s a quick example:

scores = [0,0,12,0]
print(any(scores))# True

The number 12 is truthy, so the result is True.


When to Use the any() function

The any() function is helpful when you want to answer a simple question fast, “Is there at least one match?” It keeps your code shorter and easier to read.

1) Checking if a collection contains at least one valid value

You might have a list of values, but some of them are empty strings, None, or 0. any() helps you confirm that something meaningful exists.

Example scenario: a profile form where at least one contact method is required.

2) Validating input with multiple possible rules

Sometimes a value can pass in more than one way. For example, a password might be valid if it contains a number or a special character.

Instead of writing a long if statement, any() helps you express the idea of “one of these conditions must be true.”

3) Searching for a match without writing a manual loop

If you want to know if something exists in a list (a keyword, a permission, a status), any() can stop early when it finds a match.

That early exit is great for performance, especially when your iterable is large.

4) Working with results from generators

any() pairs really well with generator expressions. You can test a condition without building a full list in memory.

If you’ve ever written something like “check if any item matches this rule,” this is exactly the tool you want.


Examples of the any() function

Let’s go through a few common, practical examples. Each one shows a slightly different way to use any().

Example 1: Check if any number is negative

temperatures = [3,8, -2,6]

has_negative =any(temp <0for tempin temperatures)
print(has_negative)# True

This reads naturally: “any temperature less than 0.”


Example 2: Detect if a message contains banned words

You can scan a message for a set of words that should not appear.

banned_words = ["spam","scam","fake"]
message ="This deal is totally real, not a scam."

has_banned_word =any(wordin message.lower()for wordin banned_words)
print(has_banned_word)# True

That style is great for quick moderation checks.


Example 3: Check if a user has any admin-level permissions

user_permissions = {"read","comment","upload"}

admin_permissions = {"delete","ban_user","edit_settings"}

is_admin =any(permissionin user_permissionsfor permissionin admin_permissions)
print(is_admin)# False

Instead of comparing everything by hand, any() makes it simple.


Example 4: Confirm at least one form field was filled

Imagine a contact form where the user can give an email or a phone number, but not necessarily both.

email =""
phone ="555-0164"

has_contact_info =any([email, phone])
print(has_contact_info)# True

In Python, non-empty strings are truthy, and empty strings are falsy, so this works nicely.


Example 5: Check if any file looks like an image

filenames = ["notes.txt","avatar.png","report.pdf"]

has_image =any(name.endswith((".png",".jpg",".jpeg",".gif"))for namein filenames)
print(has_image)# True

That’s a clean way to detect supported formats.


Learn More About the any() function

Once you understand the basics, it helps to know how any() behaves in edge cases and how it compares to similar tools.

Truthy and falsy values matter

any() depends on truthiness, not strict boolean values.

Here are examples of falsy values:

  • False
  • None
  • 0 or 0.0
  • "" (empty string)
  • [], {}, set() (empty collections)

Example:

values = [0,"",None, []]
print(any(values))# False

All values are falsy, so any() returns False.

Now compare:

values = [0,"",None, [1]]
print(any(values))# True

The list [1] is not empty, so it counts as truthy.


any() returns False for empty iterables

This sometimes surprises beginners:

empty_list = []
print(any(empty_list))# False

It makes sense if you think about it like this: “Does at least one item exist that is truthy?”

No items exist, so the answer is no.


any() short-circuits (stops early)

any() does not check every element if it doesn’t have to.

Example:

numbers = [0,0,0,5,0,0]

result =any(n >0for nin numbers)
print(result)# True

As soon as Python reaches 5, it stops evaluating the generator expression.

This can make any() much faster than scanning the entire list manually.


any() vs all()

People often learn these two together because they feel like opposites.

  • any() returns True if at least one item is truthy
  • all() returns True if every item is truthy

Example:

flags = [True,True,False]

print(any(flags))# True
print(all(flags))# False

So if you want “one success is enough,” use any().

If you want “everything must pass,” use all().


Using any() with a list vs a generator

Both are valid:

any([x >10for xin numbers])

and:

any(x >10for xin numbers)

The generator version is usually better because it avoids creating a full list in memory.

In other words, this:

any(x >10for xin numbers)

is cleaner and more efficient for large datasets.


Common mistake: calling any() on a string

Strings are iterables too, so any() will check characters one by one.

text ="Hi"
print(any(text))# True

That happens because "H" and "i" are both truthy characters.

If your goal was to check if a string is empty, use a direct check:

text =""
print(bool(text))# False

Or in an if statement:

if text:
print("Text is not empty")


Using any() to check multiple independent conditions

Sometimes you have multiple checks and want to know if any of them pass.

Example: allow sign-in if the user has a verified email OR verified phone OR a trusted device.

email_verified =False
phone_verified =True
trusted_device =False

can_sign_in =any([email_verified, phone_verified, trusted_device])
print(can_sign_in)# True

That pattern reads like plain English.


any() inside a larger condition

You can combine any() with other checks to build more realistic rules.

Example: the order can ship if it is paid and at least one item is in stock.

is_paid =True
stock = [0,0,3]

can_ship = is_paidandany(count >0for countin stock)
print(can_ship)# True

That line stays readable, even with real logic.


Debugging tip: print what you’re checking

If any() returns something unexpected, check your iterable and your condition.

Example:

names = ["","  ","Lia"]
print([name.strip()for namein names])# ['', '', 'Lia']

has_real_name =any(name.strip()for namein names)
print(has_real_name)# True

strip() removes spaces, so " " becomes falsy.


Summary

The any() function is one of the easiest ways to check if at least one value or condition is truthy. You can use it for validation, quick searches, permission checks, and cleaner conditional logic, especially with generator expressions. Once you get comfortable with truthy and falsy values, any() becomes a tool you’ll reach for constantly.