2. Lists and Strings are Similar

Lists, like strings, are collection data types. Many of the operations you learned to do on strings in the last chapter you can do on lists. In this lesson we’ll review a few of these similarities.

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 length of the top-most list. In other words, sublists are considered to be a single item when counting the length of the list.

List Membership

in and not in are boolean operators that test membership in a sequence. We used them previously with strings and they also work here.

List Slices

The slice operation we saw with strings also work on lists. Remember that the first index is the starting point for the slice and the second number is one index past the end of the slice (up to but not including that element). Recall also that if you omit the first index (before the colon), the slice starts at the beginning of the sequence. If you omit the second index, the slice goes to the end of the sequence.

List Deletion

Although there are many similarities between lists and strings, there are some important differences. Perhaps the most profound is that lists are mutable (can be changed), where as strings are immutable. An example of this can be seen in list deletion — an operation that wouldn’t be possible on a string, since its collection of characters can’t be changed.

Using slices to delete list elements can be awkward and error-prone, so Python provides an alternative that is more readable. The del statement removes an element from a list by using its position.

As you might expect, del handles negative indices and causes a runtime error if the index is out of range. In addition, you can use a slice as an index for del. As usual, slices select all the elements up to, but not including, the second index.

Check your understanding

    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.

    What is printed by the following statements?

    alist = [3, 67, "cat", [56, 57, "dog"], [ ], 3.14, False]
    print(len(alist))
    
  • 7
  • Yes, there are 7 items in this list even though two of them happen to also be lists.
  • 8
  • len returns the number of top level items in the list. It does not count items in sublists.

    What is printed by the following statements?

    alist = [3, 67, "cat", [56, 57, "dog"], [ ], 3.14, False]
    print(3.14 in alist)
    
  • True
  • Yes, 3.14 is an item in the list alist.
  • False
  • There are 7 items in the list, 3.14 is one of them.

    What is printed by the following statements?

    alist = [3, 67, "cat", [56, 57, "dog"], [ ], 3.14, False]
    print(57 in alist)
    
  • True
  • in returns True for top level items only. 57 is in a sublist.
  • False
  • Yes, 57 is not a top level item in alist. It is in a sublist.

    What is printed by the following statements?

    alist = [3, 67, "cat", [56, 57, "dog"], [ ], 3.14, False]
    print(alist[4:])
    
  • [ [ ], 3.14, False]
  • Yes, the slice starts at index 4 and goes up to and including the last item.
  • [ [ ], 3.14]
  • By leaving out the upper bound on the slice, we go up to and including the last item.
  • [ [56, 57, "dog"], [ ], 3.14, False]
  • Index values start at 0.