Skip to content
This repository has been archived by the owner on Apr 1, 2021. It is now read-only.

Add Python any article #827

Merged
3 commits merged into from
Apr 30, 2016
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions python-any.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Python any(iterable)
Copy link
Member

@koustuvsinha koustuvsinha Apr 30, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Python any(iterable)


`any()` is a built-in function in Python 3, to check if any of the items of an [_iterable_](https://docs.python.org/3/glossary.html#term-iterable) is `True`. It takes one argument, `iterable`.

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

## Return Value
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)

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

## Code Sample

```python
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
```
:rocket: [REPL It!](https://repl.it/CL9c/0)

[Documentation](https://docs.python.org/3/library/functions.html#any)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Documentation Official Docs