def count(iter, key=None):
    if key:
        if callable(key):
            return sum(bool(key(x)) for x in iter)
return sum(x == key for x in iter)
try:
    return len(iter)
except TypeError:
    return sum(1 for _ in iter)
Length of generator output