8.1. Idiom Any
Return True if any element of the iterable is true.
If the iterable is empty, return False.
Built-in
8.1.1. Problem
>>> data = [True, False, True]
>>>
>>> result = False
>>> for x in data:
... if x is True:
... result = True
>>>
>>> result
True
8.1.2. Solution
>>> data = [True, False, True]
>>> result = any(data)
>>>
>>> result
True
8.1.3. Implementation
>>> def any(iterable):
... for element in iterable:
... if element:
... return True
... return False
8.1.4. Case Study 1
users = [
{'firstname': 'Mark', 'lastname': 'Watney', 'age':40},
{'firstname': 'Melissa', 'lastname': 'Lewis', 'age':41},
{'firstname': 'Rick', 'lastname': 'Martinez', 'age':39},
{'firstname': 'Alex', 'lastname': 'Vogel', 'age':15},
]
if all(user['age']>=18 for user in users):
print('All users are adult')
else:
print('We have at least one child')
# We have at least one child
8.1.5. Case Study 2
users = [
{'firstname': 'Mark', 'lastname': 'Watney', 'is_staff': True},
{'firstname': 'Melissa', 'lastname': 'Lewis', 'is_staff': True},
{'firstname': 'Rick', 'lastname': 'Martinez', 'is_staff': True},
{'firstname': 'Alex', 'lastname': 'Vogel', 'is_staff': False},
]
if all(user['is_staff'] for user in users):
print('All users are staff')
else:
print('We have at least one non-staff')
# We have at least one non-staff
8.1.6. Use Case - 1
>>> any(x for x in range(0,5))
True
8.1.7. Use Case - 2
>>> users = [
... {'is_admin': True, 'name': 'Mark Watney'},
... {'is_admin': True, 'name': 'Melisa Lewis'},
... {'is_admin': False, 'name': 'Rick Martinez'},
... {'is_admin': True, 'name': 'Alex Vogel'},
... {'is_admin': False, 'name': 'Beth Johanssen'},
... {'is_admin': False, 'name': 'Chris Beck'},
... ]
>>>
>>>
>>> if any(user['is_admin'] for user in users):
... print('At least one person is an administrator')
... else:
... print('There are no administrators')
At least one person is an administrator
8.1.8. Assignments
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: Idiom Any IsAdmin
# - Difficulty: easy
# - Lines: 1
# - Minutes: 3
# %% English
# 1. Define `result: bool` with the result of checking
# if any user has admin role
# 2. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj `result: bool` z wynikiem sprawdzenia
# czy którykolwiek użytkownik ma rolę admin
# 2. Uruchom doctesty - wszystkie muszą się powieść
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> assert result is not Ellipsis, \
'Assign result to variable: `result`'
>>> assert type(result) is bool, \
'Variable `result` has invalid type, should be bool'
>>> result
True
"""
class User:
def __init__(self, firstname, lastname, role):
self.lastname = lastname
self.firstname = firstname
self.role = role
def is_admin(self):
return self.role == 'admin'
USERS = [
User('Mark', 'Watney', role='user'),
User('Melissa', 'Lewis', role='admin'),
User('Rick', 'Martinez', role='user'),
]
# Define `result: bool` with the result of checking
# if any user has admin role
# type: bool
result = ...
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: Idiom Any Impl
# - Difficulty: medium
# - Lines: 4
# - Minutes: 3
# %% English
# 1. Write own implementation of a built-in `any()` function
# 2. Define function `myany` with
# parameter `iterable: list[bool]`
# return `bool`
# 3. Don't validate arguments and assume, that user will
# always pass valid type of arguments
# 4. Do not use built-in function `any()`
# 5. Run doctests - all must succeed
# %% Polish
# 1. Zaimplementuj własne rozwiązanie wbudowanej funkcji `any()`
# 2. Zdefiniuj funkcję `myany` z parametrami:
# parametr `iterable: list[bool]`
# return `bool`
# 3. Nie waliduj argumentów i przyjmij, że użytkownik:
# zawsze poda argumenty poprawnych typów
# 4. Nie używaj wbudowanej funkcji `any()`
# 5. Uruchom doctesty - wszystkie muszą się powieść
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> from inspect import isfunction
>>> assert isfunction(myany)
>>> myany([True])
True
>>> myany([False])
False
>>> myany([True, False, True])
True
>>> myany([True, True, True])
True
>>> myany([False, False, False])
False
"""
# Write own implementation of a built-in `any()` function
# Define function `myany` with
# parameter `iterable: list[bool]`
# return `bool`
# Don't validate arguments and assume, that user will
# always pass valid type of arguments
# Do not use built-in function `any()`
# type: Callable[list[bool], bool]
def myany(iterable):
...