Do you often have to check lists for compliance with conditions? Forget about cumbersome loops! π«π
any() β returns True if at least one element is true. β
all() β returns True only if all elements are true. π
# Example: checking if there are negative numbers
numbers = [1, 5, -3, 7]
# Bad: through a loop
has_negative = False
for num in numbers:
if num < 0:
has_negative = True
# Beautiful:
has_negative = any(num < 0 for num in numbers) # True β¨