def leniter(iterator):
    """leniter(iterator): return the length of an iterator, consuming it."""
    if hasattr(iterator, "__len__"):
        return len(iterator)
    nelements = 0
    for _ in iterator:
        nelements += 1
    return nelements