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.

Check your understanding

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

    alist = [3, 67, "cat", 3.14, False]
    print(len(alist))
    
  • 4
  • len returns the actual number of items in the list, not the maximum index value.
  • 5
  • Yes, there are 5 items in this list.

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

    L = [0.34, '6', 'SI106', 'Python', -2]
    print(len(L[1:-1]))
    
  • 2
  • The list begins with the second item of L and includes everything up to but not including the last item.
  • 3
  • Yes, there are 3 items in this list.
  • 4
  • The list begins with the second item of L and includes everything up to but not including the last item.
  • 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