How about:

>>> any(isinstance(e, int) and e > 0 for e in [1,2,'joe'])
True

It also works with all() of course:

>>> all(isinstance(e, int) and e > 0 for e in [1,2,'joe'])
False
Answer from Antoine P. on Stack Overflow
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ builtins โ€บ functions.html
Built-in Functions โ€” Python 3.14.7 documentation
See also Binary Sequence Types โ€” bytes, bytearray, memoryview, Bytes Objects, and Bytes and Bytearray Operations. ... Return True if the object argument appears callable, False if not. If this returns True, it is still possible that a call fails, but if it is False, calling object will never succeed. Note that classes are callable (calling a class returns a new instance); instances are callable if their class has a __call__() method. Added in version 3.2: This function was first removed in Python 3.0 and then brought back in Python 3.2.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ ref_func_any.asp
Python any() Function
Python Examples Python Compiler ... Interview Q&A Python Training ... The any() function returns True if any item in an iterable are true, otherwise it returns False....
Discussions

Using "any" and "all" in Python
Might be worth explicitly point out that: >>> all([]) True I'm sure there are good maths reasons for this behaviour, but I found it quite surprising when I first ran into it. [edit] I know it does this and I know why it does it. I can't be convinced to go back in time and not find it surprising folks! [troll] it should clearly return None for an empty list since there is no answer to whether all values are true or not! More on reddit.com
๐ŸŒ r/Python
12
67
March 29, 2023
functional programming - any() function in Python with a callback - Stack Overflow
The Python standard library defines an any() function that Return True if any element of the iterable is true. If the iterable is empty, return False. It checks only if the elements evaluate to T... More on stackoverflow.com
๐ŸŒ stackoverflow.com
generator expression - How does this input work with the Python 'any' function? - Stack Overflow
In the python docs page for any, the equivalent code for the any() function is given as: def any(iterable): for element in iterable: if element: return True return Fals... More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - What does the builtin function any() do? - Stack Overflow
I did some google searching on how to check if a string has any elements of a list in it and I found this bit of code that works: if any(i in string for i in list): I know this works, but I don't More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
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.
๐ŸŒ
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 Morsels
pythonmorsels.com โ€บ any-and-all
Python's any() and all() functions - Python Morsels
March 29, 2023 - The any function checks for the truthiness of each item in a given iterable, but we need something a little more than that: we need to check a condition on each element. Specifically, we need to check whether a number is between 0 and 5. Python ...
๐ŸŒ
YouTube
youtube.com โ€บ watch
any() Built-In Function With Examples | Python Tutorial - YouTube
How to use the built-in any() function in Python to check if any item in an iterable object is true. Source code: https://github.com/portfoliocourses/python...
Published: December 7, 2023
Find elsewhere
๐ŸŒ
Reddit
reddit.com โ€บ r/python โ€บ using "any" and "all" in python
r/Python on Reddit: Using "any" and "all" in Python
March 29, 2023 - 67 votes, 12 comments. 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โ€ฆ
๐ŸŒ
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.
Top answer
1 of 5
193

If you use any(lst) you see that lst is the iterable, which is a list of some items. If it contained [0, False, '', 0.0, [], {}, None] (which all have boolean values of False) then any(lst) would be False. If lst also contained any of the following [-1, True, "X", 0.00001] (all of which evaluate to True) then any(lst) would be True.

In the code you posted, x > 0 for x in lst, this is a different kind of iterable, called a generator expression. Before generator expressions were added to Python, you would have created a list comprehension, which looks very similar, but with surrounding []'s: [x > 0 for x in lst]. From the lst containing [-1, -2, 10, -4, 20], you would get this comprehended list: [False, False, True, False, True]. This internal value would then get passed to the any function, which would return True, since there is at least one True value.

But with generator expressions, Python no longer has to create that internal list of True(s) and False(s), the values will be generated as the any function iterates through the values generated one at a time by the generator expression. And, since any short-circuits, it will stop iterating as soon as it sees the first True value. This would be especially handy if you created lst using something like lst = range(-1,int(1e9)) (or xrange if you are using Python2.x). Even though this expression will generate over a billion entries, any only has to go as far as the third entry when it gets to 1, which evaluates True for x>0, and so any can return True.

If you had created a list comprehension, Python would first have had to create the billion-element list in memory, and then pass that to any. But by using a generator expression, you can have Python's builtin functions like any and all break out early, as soon as a True or False value is seen.

2 of 5
51
>>> names = ['King', 'Queen', 'Joker']
>>> any(n in 'King and john' for n in names)
True

>>> all(n in 'King and Queen' for n in names)
False

It just reduce several line of code into one. You don't have to write lengthy code like:

for n in names:
    if n in 'King and john':
       print True
    else:
       print False
๐ŸŒ
YouTube
youtube.com โ€บ watch
Python's ANY and ALL Functions are Simpler Than You Think! - YouTube
๐Ÿš€ Think Python's any() and all() functions are complicated? Think again! In this tutorial, I'll show you exactly how these powerful functions work and why t...
Published: January 10, 2025
Top answer
1 of 3
6

As the docs for any say:

Return True if any element of the iterable is true. If the iterable is empty, return False. Equivalent to:

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False

So, this is equivalent to:

for element in (i in string for i in list):
    if element:
        return True
return False

โ€ฆ which is itself effectively equivalent to:

for i in list:
    element = i in string
    if element:
        return True
return False

If you don't understand the last part, first read the tutorial section on list comprehensions, then skip ahead to iterators, generators, and generator expressions.

If you want to really break it down, you can do this:

elements = []
for i in list:
    elements.append(i in string)
for element in elements:
    if element:
        return True
return False

That still isn't exactly the same, because a generator expression builds a generator, not a list, but it should be enough to get you going until you read the tutorial sections.


But meanwhile, the point of having any and comprehensions and so on is that you can almost read them as plain English:

if any(i in string for i in list): # Python

if any of the i's is in the string, for each i in the list: # pseudo-English
2 of 3
2
i in string for i in list

This produces an iterable of booleans indicating whether each item in list is in string. Then you check whether any item in this iterable of bools is true.

In effect, you're checking whether any of the items in the list are substrings of string.

๐ŸŒ
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...
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ methods โ€บ built-in โ€บ any
Python any()
Online Python Online JavaScript Online SQL Online Java Online HTML Online C Online C++ Online C# Online PHP Online Swift Online Kotlin Online TypeScript Online Go Online Rust Online Scala Online Dart Online R Online Ruby ... The any() function returns True if any element of an iterable is True.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ any-all-in-python
Any All in Python - GeeksforGeeks
July 23, 2025 - In this another example, we are seeing of all numbers in list1 are odd and by using all() function, if they are odd then we will return True otherwise False. ... # Illustration of 'all' function in python 3 # Take two lists list1=[] list2=[] # All numbers in list1 are in form: 4*i-3 for i in range(1,21): list1.append(4*i-3) # list2 stores info of odd numbers in list1 for i in range(0,20): list2.append(list1[i]%2==1) print('See whether all numbers in list1 are odd =>') print(all(list2))
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:

if (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:

>>> 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:

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

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

if 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:

>>> 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.

>>> 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:

>>> [].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:

any(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.

๐ŸŒ
Naples Daily News
naplesnews.com โ€บ story โ€บ news โ€บ environment โ€บ 2026 โ€บ 04 โ€บ 28 โ€บ burmese-pythons-eat-kill-humans-florida-snakes-invasive-species โ€บ 89819582007
Do Burmese pythons eat or kill humans? Why people are scared of snakes
April 28, 2026 - They are native to India, lower China, the Malay Peninsula, and some islands of the East Indies. They are not protected in Florida except by anti-cruelty law and can be humanely killed on private property with landowner permission.