1. Lists

A list is a sequential collection of Python data values, where each value is identified by an index. The values that make up a list are called its elements. Lists are similar to strings, which are ordered collections of characters, except that the elements of a list can have any type and for any one list, the items can be of different types.

List Basics

There are several ways to create a new list. The simplest is to enclose the elements in square brackets.

[10, 20, 30, 40]
["spam", "bungee", "swallow"]

The first example is a list of four integers. The second is a list of three strings. As we said above, the elements of a list don’t have to be the same type. The following list contains a string, a float, an integer, and another list.

["hello", 2.0, 5, [10, 20]]

A list within another list is said to be nested and the inner list is often called a sublist. We’ll talk more about nested lists below. Finally, there is a special list that contains no elements. It is called the empty list and is denoted [].

As you would expect, we can also assign list values to variables and pass lists as parameters to functions.

Accessing List Elements

The syntax for accessing the elements of a list is the same as the syntax for accessing the characters of a string. We use the index operator ( [] – not to be confused with an empty list). The expression inside the brackets specifies the index. Remember that the indices start at 0. Any integer expression can be used as an index and negative index values will locate items from the right instead of from the left.

Nested Lists

A nested list is a list that appears as an element in another list. In this list, the element with index 3 is a nested list. If we print(nested[3]), we get [10, 20]. To extract an element from the nested list, we can proceed in two steps. First, extract the nested list, then extract the item of interest. It is also possible to combine those steps using bracket operators that evaluate from left to right.

Check your understanding

    A list can contain only integer items.
  • False
  • Yes, unlike strings, lists can consist of any type of Python data.
  • True
  • Lists are heterogeneous, meaning they can have different types of data.

    What is printed by the following statements?

    alist = [3, 67, "cat", [56, 57, "dog"], [ ], 3.14, False]
    print(alist[5])
    
  • [ ]
  • The empty list is at index 4.
  • 3.14
  • Yes, 3.14 is at index 5 since we start counting at 0 and sublists count as one item.
  • False
  • False is at index 6.

    What is printed by the following statements?

    alist = [3, 67, "cat", [56, 57, "dog"], [ ], 3.14, False]
    print(alist[2].upper())
    
  • Error, you cannot use the upper method on a list.
  • alist[2] is the string cat so the upper method is legal
  • 2
  • 2 is the index. We want the item at that index.
  • CAT
  • Yes, the string cat is upper cased to become CAT.

    What is printed by the following statements?

    alist = [ [4, [True, False], 6, 8], [888, 999] ]
    if alist[0][1][0]:
       print(alist[1][0])
    else:
       print(alist[1][1])
    
  • 6
  • 6 is in the wrong list. alist[1] refers to the second item in alist, namely [888,999].
  • 8
  • 8 is in the wrong list. alist[1] refers to the second item in alist, namely [888,999].
  • 888
  • Yes, alist[0][1][0] is True and alist[1] is the second list, the first item is 888.
  • 999
  • alist[0][1][0] is True. Take another look at the if statement.