In Python programs we often need to get the size or count of elements. In strings we need a length. The len()
built-in helps here.
Often we can optimize performance with len
. The number of elements is stored on the object, not calculated, so len
is fast.
Len returns the number of characters in a string
. It counts spaces, punctuation, all characters the same. We must be careful with taking the len
of a None
variable—this fails.
len
call tests an empty string
. This string
has zero characters but is not None
.NoneType
has no len
built-in support.# Has length of 3: value = "cat" print(len(value)) # Has length of 0: value = "" print(len(value)) # Causes TypeError: value = None print(len(value))3 0 Traceback (most recent call last): File "C:\programs\file.py", line 13, in <module> print(len(value)) TypeError: object of type 'NoneType' has no len()
List
, dictionaryLen()
returns the number of elements in a collection. For a collection with nested, sub-collections, counting is shallow: not all nested elements are considered.
Keys
and values are not independent.# Get length of list with len. elements = [1, 2, 3] print(len(elements)) # Get length of tuple. items = ("cat", "dog", "bird", "shark") print(len(items)) # Length of example set (key count). set = {100, 200, 300} print(len(set)) # Length of dictionary (pair count). lookup = {"cat" : 4, "centipede" : 100} print(len(lookup))3 4 3 2
Let us revisit nested collections. A collection itself is an element, so it counts just one time. The len
built-in does not recurse—it is simple and does not even loop.
# A nested list: list = [1, 2, [4, 5]] # Shallow count of elements. print(len(list)) print(len(list[2]))3 2
We cannot just take the len
of any variable. This program attempts to take the length of an int
variable. And it fails, miserably, with a TypeError
that ends its operation.
len()
counts countable units: chars in a string
, elements in a list. A number has digits, but no other "units."value = 100 # Cannot take length of int: length = len(value)Traceback (most recent call last): File "C:\programs\file.py", line 6, in <module> length = len(value) TypeError: object of type 'int' has no len()
The len
of collections and strings is stored as a number in memory. It is not computed, as in a loop, each time it is accessed. For this reason, len
is much faster than a loop.
string
with len
in a loop. This is timed.for
-loop version. The end result is the same (each character is counted).len
. The for
-loop is useful only when counting chars, where their values matter.import time value = "characters" print(time.time()) # Version 1: len for i in range(0, 1000000): length = len(value) if length != 10: raise Exception() print(time.time()) # Version 2: count chars for i in range(0, 1000000): length = 0 for c in value: length += 1 if length != 10: raise Exception() print(time.time())1406752804.325871 1406752804.606887 len = 0.281 s 1406752806.05097 for-loop = 1.444 s
A length cannot be negative. So we can use len
as a loop boundary: this is a convenient way to loop over a list. But when not needed, avoiding len
is ideal.
Consider a for-in
loop to avoid using len
—this loop construct will enumerate each element in a collection, and no indexes are needed.