Lists

A list is a tool for storing a sequence of values. The individual values contained within a list are called the elements or items of the list.

To define a list in Python, we write the elements of the list between a pair of square braces, separated by commas.In the cell below, we create a list containing strings representing the names of each of the eight planets in our solar system. We will store the list in the variable planets.

planets = ['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune']

List objects have a data type, just like every other object in Python. In the following cell, we check the type of planets.

type(planets)
list

We can also print lists.

print(planets)
['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune']

Accessing List Elements

Elements in a list are assigned an order when the list is defined, and each element being assigned a numerical position called an index. The first element is assigned an index of 0, the second element is assigned an index of 1, the third element is assigned an index of 2, and so on.

Assume you have a list called my_list, and you wish to access an element whose index is stored in a variable i. This can by done using the following code: my_list[i].

# Print the 1st element of planets
print(planets[0])
Mercury
# Print the 3rd element of planets
print(planets[2])
Earth
# Print the 8th element of planets
print(planets[7])
Neptune

We will get an error if we try to access a list element that does not exist.

print(planets[8])
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-7-c5c892610201> in <module>
----> 1 print(planets[8])

IndexError: list index out of range

We can also access elements of a list by counting from the end of the list. This is accomplished through the use of negative indices. The last element in the list is a index -1, the second to last is at index -2, and so on.

# Print the last element of planets.
print(planets[-1])
# Print the 3rd from last element of planets. 
print(planets[-3])

In the cell below, write two lines of code that each print out “Mars”. Use a positive index in the first line and a negative index in the second line.

print(planets[3])
print(planets[-5])

Types of List Elements

While a list object is of data type list, the individual elements of a list will each have their own data types. For example, the elements of the elements of planets list we defined previously are all of type str.

print(type(planets))
print(type(planets[4]))

It is possible to create lists that contain other data types. The following list contains elements of type int.

pi_digits = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]
print(pi_digits[5])

This list contains elements of type bool.

bool_list = [False, True, True, False, True]
print(bool_list[3])

It is even possible for a single list to contains elements of several different data types.

varied_list = [False, "one", 2, 3.0]
print(type(varied_list))
print(type(varied_list[0]))
print(type(varied_list[1]))
print(type(varied_list[2]))
print(type(varied_list[3]))

In fact, it is even possible for lists to contain other lists! We will further discuss this concept soon.