Execute Program

Python for Programmers: Any and All

Welcome to the Any and All lesson!

This lesson is shown as static text below. However, it's designed to be used interactively. Click the button below to start!

  • Python comes with a pair of built-in functions for working with iterables, like lists or tuples, that contain booleans or other truthy and falsey values. The any function asks whether any value in an iterable is truthy.

  • >
    result = any([
    "a" + "b" == "abb",
    "x" + "y" == "z",
    ])
    result
    Result:
    FalsePass Icon
  • >
    account_balances = [-100, 200, 300]
    below_zero = [balance < 0 for balance in account_balances]
    overdraft_handling_required = any(below_zero)
    overdraft_handling_required
    Result:
    TruePass Icon
  • any asks whether at least one element is truthy. But if there are no elements, then there are no elements that are truthy.

  • >
    any([])
    Result:
    FalsePass Icon
  • The fact that any([]) == False may seem arbitrary at first, but it's not. Writing a simple implementation of any ourselves shows why it works this way. We filter the list down to only the truthy values, then ask whether that list has any elements.

  • >
    def custom_any(values):
    truthy_values = [value for value in values if value]
    return len(truthy_values) > 0
  • Note: this code example reuses elements (variables, etc.) defined in earlier examples.
    >
    custom_any([True, False, True])
    Result:
    TruePass Icon
  • Note: this code example reuses elements (variables, etc.) defined in earlier examples.
    >
    custom_any([False, False, False])
    Result:
    FalsePass Icon
  • Note: this code example reuses elements (variables, etc.) defined in earlier examples.
    >
    custom_any([])
    Result:
    FalsePass Icon
  • If you ever forget whether any([]) is True or False, think back to the implementation above!

  • The all function asks whether every value in an iterable is truthy.

  • >
    result = all([
    -3 <= 4,
    7 <= 10,
    2 == 2.0,
    ])
    result
    Result:
    TruePass Icon
  • >
    account_balances = [100, 200, -50]
    above_zero = [balance >= 0 for balance in account_balances]
    all_accounts_in_good_standing = all(above_zero)
    all_accounts_in_good_standing
    Result:
    FalsePass Icon
  • The all function is true for empty iterables, which is the opposite of any's behavior.

  • >
    all([])
    Result:
    TruePass Icon
  • Once again, we can remember this by thinking about a simple implementation that filters the values.

  • >
    def custom_all(values):
    truthy_values = [value for value in values if value]
    return len(truthy_values) == len(values)
  • Note: this code example reuses elements (variables, etc.) defined in earlier examples.
    >
    custom_all([True, True, True])
    Result:
    TruePass Icon
  • Note: this code example reuses elements (variables, etc.) defined in earlier examples.
    >
    custom_all([True, False, True])
    Result:
    FalsePass Icon
  • Note: this code example reuses elements (variables, etc.) defined in earlier examples.
    >
    custom_all([])
    Result:
    TruePass Icon
  • When we call custom_all([]), we're asking "is the number of truthy values the same as the original number of elements?" Both have 0 elements, so yes!

  • any and all work with any iterable data type (any data type that we can use in a for loop).

  • When we iterate over a dictionary with a for loop, we get the dictionary's keys. Likewise, if we pass a dictionary to any or all, we're asking "are any/all of this dictionary's keys (not values!) truthy?"

  • >
    result = any({
    True: 100,
    False: 200,
    })
    result
    Result:
    TruePass Icon
  • >
    result = any({
    False: 100,
    0: 200,
    })
    result
    Result:
    FalsePass Icon
  • >
    result = all({
    "a string": 100,
    "another string": 200,
    })
    result
    Result:
    TruePass Icon
  • Here's a code problem:

    Write a function that decides whether a password meets all of our password requirements. We've already written functions that check three different validation rules. Each function returns a boolean.

    You can solve this by calling all on a list comprehension, where the list comprehension loops over validators, calling each one on the password. Don't forget to pass the password to each function!

    def at_least_4_characters(password):
    return len(password) >= 4

    def shorter_than_20_characters(password):
    return len(password) < 20

    def contains_special_characters(password):
    # `.isalnum()` is true when the string is "alphanumeric", meaning it only
    # contains letters and numbers.
    return not password.isalnum()

    validators = [
    at_least_4_characters,
    shorter_than_20_characters,
    contains_special_characters,
    ]
    def password_allowed(password):
    return all([validator(password) for validator in validators])
    assert not password_allowed("abc")
    assert not password_allowed("hunter2")
    assert not password_allowed("no one will ever guess this long password!")
    assert password_allowed("M!lesP0wer")
    assert password_allowed("@HereYouGo@")
    Goal:
    None
    Yours:
    NonePass Icon