๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ ref_func_any.asp
Python any() Function
Python Examples Python Compiler ... Certificate Python Training ... The any() function returns True if any item in an iterable are true, otherwise it returns False....
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-any-function
Python any() function - GeeksforGeeks
July 23, 2025 - In this example, the any() function in Python checks for any element satisfying a condition and returns True in case it finds any True value. This function is particularly useful to check if all/any elements in List meet condition in Python.
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ library โ€บ functions.html
Built-in Functions โ€” Python 3.14.3 documentation
2 weeks ago - Convert an integer number to a binary string prefixed with โ€œ0bโ€. The result is a valid Python expression. If integer is not a Python int object, it has to define an __index__() method that returns an integer.
๐ŸŒ
DataCamp
datacamp.com โ€บ tutorial โ€บ python-any-function
Python any() Function: Guide With Examples and Use Cases | DataCamp
July 31, 2024 - The any() function in Python returns True if at least one element in an iterable (list, tuple, set, etc.) is true, and False otherwise.
๐ŸŒ
Mostly Python
mostlypython.com โ€บ using-any
Using `any()`
January 23, 2025 - It feels like I should be able to use the one-liner approach without always going through a round of refactoring. It turns out using any() in the real world is a bit more complicated than it appears on the surface. Python makes it quite straightforward to determine if a specific item appears in a collection.
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ python-any-and-all-functions-explained-with-examples
Python any() and all() Functions โ€“ Explained with Examples
August 10, 2021 - When coding in Python, have you ever had to check if any item or all items in an iterable evaluate to True? The next time you need to do so, be sure to use the nifty functions any() and all(). In this tutorial, we'll learn about Python's any() and al...
๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python
How to Use all() and any() in Python | note.nkmk.me
May 12, 2025 - In Python, you can use the built-in functions all() and any() to check whether all elements or at least one element in an iterable (such as a list or tuple) evaluate to True. Built-in Functions - all( ...
๐ŸŒ
Real Python
realpython.com โ€บ any-python
How to Use any() in Python โ€“ Real Python
February 18, 2022 - In the above example, you check each applicantโ€™s credentials and schedule an interview if the applicant meets any of your three criteria. Technical Detail: Pythonโ€™s any() and or arenโ€™t limited to evaluating Boolean expressions. Instead, Python performs a truth value test on each argument, evaluating whether the expression is truthy or falsy.
Find elsewhere
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ methods โ€บ built-in โ€บ any
Python any()
Become a certified Python programmer. Try Programiz PRO! ... The any() function returns True if any element of an iterable is True.
๐ŸŒ
Python Morsels
pythonmorsels.com โ€บ any-and-all
Python's any() and all() functions - Python Morsels
March 29, 2023 - Need to check whether all items in a list match a certain condition? You can use Python's built-in any and all functions for that!
๐ŸŒ
Reddit
reddit.com โ€บ r/python โ€บ using "any" and "all" in python
r/Python on Reddit: Using "any" and "all" in Python
March 29, 2023 - ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() in Python
Top answer
1 of 5
2

You should use .any() on a boolean array after doing the comparison, not on the values of popul_num themselves. It will return True if any of the values of the boolean array are True, otherwise False.

In fact, .any() tests for any "truthy" values, which for integers means non-zero values, so it will work on an array of integers to test if any of them are non-zero, which is what you are doing, but this is not testing the thing that you are interested in knowing. The code then compounds the problem by doing an < 0 test on the boolean value returned by any, which always evaluates True because boolean values are treated as 0 and 1 (for False and True respectively) in operations involving integers.

You can do:

Copyif (popul_num < 0).any():
    do_whatever

Here popul_num < 0 is a boolean array containing the results of element-by-element comparisons. In your example:

Copy>>> popul_num < 0
array([False, False, False, False], dtype=bool)

You are, however, correct to use array.any() (or np.any(array)) rather than using the builtin any(). The latter happens to work for a 1-d array, but would not work with more dimensions. This is because iterating e.g. over a 4d array (which is what the builtin any() would do) gives a sequence of 3d arrays, not the individual elements.

There is also similarly .all(). The above test is equivalent to:

Copyif not (popul_num >= 0).all():
2 of 5
1

The any method of numpy arrays returns a boolean value, so when you write:

Copyif popul_num.any() < 0:

popul_num.any() will be either True (=1) or False (=0) so it will never be less than zero. Thus, you will never enter this if-statement.

What any() does is evaluate each element of the array as a boolean and return whether any of them are truthy. For example:

Copy>>> np.array([0.0]).any()
False

>>> np.array([1.0]).any()
True

>>> np.array([0.0, 0.35]).any()
True

As you can see, Python/numpy considers 0 to be falsy and all other numbers to be truthy. So calling any on an array of numbers tells us whether any number in the array is nonzero. But you want to know whether any number is negative, so we have to transfrom the array first. Let's introduce a negative number into your array to demonstrate.

Copy>>> popul_num = np.array([200, 100, 0, -1])
>>> popul_num < 0  # Test is applied to all elements in the array
np.ndarray([False, False, False, True])
>>> (popul_num < 0).any()
True

You asked about any on lists versus arrays. Python's builtin list has no any method:

Copy>>> [].any()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'any'

There is a builtin function (not method since it doesn't belong to a class) called any that serves the same purpose as the numpy .any method. These two expressions are logically equivalent:

Copyany(popul_num < 0)

(popul_num < 0).any()

We would generally expect the second one to be faster since numpy is implemented in C. However only the first one will work with non-numpy types such as list and set.

๐ŸŒ
Vultr Docs
docs.vultr.com โ€บ python โ€บ built-in โ€บ any
Python any() - Check Any True Values | Vultr Docs
September 27, 2024 - The any() function in Python checks if any element in an iterable is True. This function is extremely useful when you need a quick way to evaluate if at least one of the conditions in a composite query returns True.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ any-all-in-python
Any All in Python - GeeksforGeeks
July 23, 2025 - Any and All are two built-in functions provided in Python used for successive And/Or.
๐ŸŒ
Pytut
pytut.com โ€บ any
Python any() Function - PyTut
Python any() function takes a sequence as input (such as a tuple or list) and checks if at least one item is True.
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ python โ€บ python_any_function.htm
Python any() Function
The Python any() function is a built-in function that returns True if any of the elements of a given iterable, such as a list, tuple, set, or dictionary is truthy, otherwise, it returns False.
๐ŸŒ
Analytics Vidhya
analyticsvidhya.com โ€บ home โ€บ any() and all() in python with examples
any() and all() in Python with Examples - Analytics Vidhya
April 1, 2024 - Master any() and all() functions in Python to efficiently handle collections like lists and tuples. Understand functions with examples.
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ any-and-all-in-python-with-examples
any() and all() in Python with Examples
September 11, 2023 - The any(iterable) and all(iterable) are built-in functions in Python and have been around since Python 2.5 was released. Both functions are equivalent to writing a series of or and and operators respectively between each of the elements of the passed iterable.