-
Notifications
You must be signed in to change notification settings - Fork 0
Python Function Any
any()
is a built-in function in Python 3, to check if any of the items of an iterable is True
. It takes one argument, iterable
.
The iterable
argument is the collection whose entries are to be checked. It can typically be a list
, str
, dict
, tuple
etc., even a file object
.
The return value would be a boolean. If and only if all entries of iterable are False
, or the iterable
is empty; it returns False
. This function essentially performs a Boolean OR
operation over all elements.
If even one of them is True
, it would return True
.
The any()
operation is equivalent to (internally, may not be implemented exactly like this)
def any(iterable):
for element in iterable:
if element:
return True
return False
print(any([])) #=> False
print(any({})) #=> False
print(any([6, 7])) #=> True
print(any([6, 7, None])) #=> True
print(any([0, 6, 7])) #=> True
print(any([9, 8, [1, 2]])) #=> True
🚀 Run Code
Learn to code and help nonprofits. Join our open source community in 15 seconds at http://freecodecamp.com
Follow our Medium blog
Follow Quincy on Quora
Follow us on Twitter
Like us on Facebook
And be sure to click the "Star" button in the upper right of this page.
New to Free Code Camp?
JS Concepts
JS Language Reference
- arguments
- Array.prototype.filter
- Array.prototype.indexOf
- Array.prototype.map
- Array.prototype.pop
- Array.prototype.push
- Array.prototype.shift
- Array.prototype.slice
- Array.prototype.some
- Array.prototype.toString
- Boolean
- for loop
- for..in loop
- for..of loop
- String.prototype.split
- String.prototype.toLowerCase
- String.prototype.toUpperCase
- undefined
Other Links