Python has many built-in functions, and len()
is one of them.
Return the length (the number of items) of an object. The argument may be a sequence (such as a string, bytes, tuple, list, or range) or a collection (such as a dictionary, set, or frozen set).
If collections and sequences are objects, they could hold a length
attribute that can be updated every time something changes. Accessing this attribute would be a fast way to retrieve the collection's length.
Another approach is to iterate through the collection and count the number of items on the fly.
How does len()
calculates said length? Through iteration or attribute access? One, none, both, other approaches?