Added explanation and examples (#25859)

Updated the article with examples and difference table for clear understanding.
This commit is contained in:
Aman Ranjan Verma
2018-11-25 13:52:20 +05:30
committed by Randell Dawson
parent e2e566203c
commit 3d3d1ed068

View File

@ -1,8 +1,12 @@
--- ---
title: Python Any Iterable title: Python Any Iterable
--- ---
`any()` is a built-in function in Python 3 (and Python 2 since version 2.5), to check if any of the items of an <a href='https://docs.python.org/3/glossary.html#term-iterable' target='_blank' rel='nofollow'>_iterable_</a> is `True`. It takes one argument, `iterable`. `any()` is a built-in function in Python 3 (and Python 2 since version 2.5), to check if any of the items of an <a href='https://docs.python.org/3/glossary.html#term-iterable' target='_blank' rel='nofollow'>_iterable_</a> is `True`. It takes one argument, `iterable`. This table below is helpful in understanding `all` and `any` iterable.
<p align="center">
<img src="https://www.geeksforgeeks.org/wp-content/uploads/All-any-in-python.png" alt="All Vs Any" width="350" />
</center>
## Argument ## Argument
### iterable ### iterable
@ -11,7 +15,7 @@ The `iterable` argument is the collection whose entries are to be checked. It ca
## Return Value ## Return Value
The return value is 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. The return value is a Boolean. It returns 'False' if and only if **all** entries of iterable are `False`, or the `iterable` is empty. This function essentially performs a Boolean `OR` operation over all elements.
If even one of them is `True`, it returns `True`. If even one of them is `True`, it returns `True`.
@ -22,6 +26,8 @@ The `any()` operation is equivalent to (internally, may not be implemented exact
if element: if element:
return True return True
return False return False
It stops the execution as soon as the result is known.
## Code Sample ## Code Sample
@ -33,7 +39,13 @@ The `any()` operation is equivalent to (internally, may not be implemented exact
print(any([6, 7, None])) #=> True print(any([6, 7, None])) #=> True
print(any([0, 6, 7])) #=> True print(any([0, 6, 7])) #=> True
print(any([9, 8, [1, 2]])) #=> True print(any([9, 8, [1, 2]])) #=> True
print(any([None, []])) #=> False
print(any([9, False, [1, 2]])) #=> True
<a href='https://repl.it/CL9c/0' target='_blank' rel='nofollow'>Run Code</a> 🚀 <a href='https://repl.it/@arverma/GleefulCanineAmericanrobin' target='_blank' rel='nofollow'>Run Code</a>
<a href='https://docs.python.org/3/library/functions.html#any' target='_blank' rel='nofollow'>Official Docs</a> <a href='https://docs.python.org/3/library/functions.html#any' target='_blank' rel='nofollow'>Official Docs</a>
### Sources
1. <a href='https://www.geeksforgeeks.org/any-all-in-python/' target='_blank'>Any & All in Python, Accessed: October 22, 2018</a>
2. <a href='https://www.geeksforgeeks.org/wp-content/uploads/All-any-in-python.png' target='_blank'>Image, Accessed: October 22, 2018</a>