Python has many built-in functions , but len()one of them.
Returns the length (number of elements) of an object. The argument can be a sequence (for example, a string, bytes, a tuple, list, or range) or a collection (for example, a dictionary, set, or frozen set).
If collections and sequences are objects, they can contain an attribute lengththat can be updated every time something changes. Accessing this attribute would be a quick way to get the length of the collection.
Another approach is to iterate through the collection and count the number of elements on the fly.
How does it len()calculate the specified length? Through iteration or access to attributes? One, none, both, other approaches?
source
share