10.3. List Length¶
As with strings, the function len
returns the length of a list (the number
of items in the list). However, since lists can have items which are themselves lists, it important to note
that len
only returns the top-most length. In other words, sublists are considered to be a single
item when counting the length of the list.
1
alist = ["hello", 2.0, 5, [10, 20]]
2
print(len(alist))
3
print(len(['spam!', 1, ['Brie', 'Roquefort', 'Pol le Veq'], [1, 2, 3]]))
4
(chp09_01a)
Check your understanding
list-3-1: What is printed by the following statements?
alist = [3, 67, "cat", 3.14, False]
print(len(alist))
list-3-2: What is printed by the following statements?
alist = [3, 67, "cat", [56, 57, "dog"], [ ], 3.14, False]
print(len(alist))