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 sequences (e.g., strings), it important to note that len only returns the top-most length.

Note that alist[0] is the string "hello", which has length 5.

rec-5-18: What is printed by the following statements?

alist = [3, 67, "cat", 3.14, False]
print len(alist)

• (A) 4
• len returns the actual number of items in the list, not the maximum index value.
• (B) 5
• Yes, there are 5 items in this list.

rec-5-19: What is printed by the following statements?

L = [0.34, '6', 'SI106', 'Python', -2]
print len(L[1:-1])

• (A) 2
• The list begins with the second item of L and includes everything up to but not including the last item.
• (B) 3
• Yes, there are 3 items in this list.
• (C) 4
• The list begins with the second item of L and includes everything up to but not including the last item.
• (D) 5
• The list begins with the second item of L and includes everything up to but not including the last item.
Next Section - Accessing Elements