Lists

Is it possible to assign multiple values to a variable?

Imagine that you are writing software for your online store and reaching the point where you need to program your shopping cart. With your current Python knowledge, are you able to create a variable which will enable you to store multiple products?

No, because the variable types you know only allow you to put a single object "in a box". Programming languages, such as Python, deal with this problem by providing programmers with so-called lists.

A list is a special type of variable that allows you to "throw more than one thing into the box".

# The current content of my pocket (numbers are coins)
my_pocket = ["phone", 0.05, 0.05, 0.02, "old receipt"]
print(my_pocket)

The variable my_pocket contains a collection of different elements. The data type of this variable is a list. Note the square brackets: it opens and closes the list. Inside the brackets there are subsequent elements of the list, separated by commas. Note that these elements are not of the same type - in our example, they are strings and numbers, but in fact, each element in the list can be of any type: number, string, logical value (True/False), None, and even another list!

The list is organized. This means that on the first place of our sample list there is the word "phone", on the last place there is the word "old receipt". Each element has its own position in the list, which programmers call an index.

Now focus, because there is an important thing to remember: we number the list starting from zero, instead of one! This means that the index of the "phone" element is 0, the number 0.05 - 1 and so on until the "old receipt", which has the index 4!

The easiest way to remember this is to compare the list to a tenement house, as in the example below:

#221b Baker Street!
baker_street_221b = ["Mrs Hudson", "Holmes", "Watson"]
print(baker_street_221b)

See the example of a list

Mrs Hudson lives on the ground floor (i.e. the floor with number 0), Sherlock Holmes on the first floor, and Dr. Watson on the second floor.

Can I retrieve a single element?

Absolutely! You just need to know the number of the element in the list. If in the example with Sherlock we want to download Dr. Watson's data, we need to know the number of the floor on which he lives. Doctor lives on the 2nd floor, his index on the list is therefore 2.

# display Dr. Watson's data
print(baker_street_221b[2])

See the example of retrieving an element

Note that we displayed the doctor's data by entering the name of the variable from the list, and then immediately, in square brackets, we followed it with the index of the element we wanted to retrieve. Similarly, we will display the data of Mrs. Hudson from the ground floor like this:

# Mrs Hudson
mrs_hudson = baker_street_221b[0]
print(mrs_hudson)

See the of accessing an element

Isn't it simple?

Can I add a new element to the list?

Yes, just use the append() method.

baker_street_221b.append(Lestrade)
print(baker_street_221b)

See the of adding a new element

Inspector Lestrade moved into Mrs. Hudson's house. He took up another floor and... our analogy is falling apart here, because in Python it looks like every new element lengthens the list. So it's as if every tenant moving out demolished their floor and every new one added another one. ;)

How many elements are in the list?

To check it, just use the len() function, where the name of the list is given as a parameter:

See how to checki the number of list elements

print(len(baker_street_221b))

Is it possible to retrieve an element that does not exist?

Of course not! If we ask Python about the inhabitant of the tenth floor:

# 10 floor!
print(baker_street_221b[10])

We get:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range

Task 1

Declare a list named the_beatles. Include the following members in the list: "John Lennon', 'Paul McCartney', 'George Harrison', 'Stuart Sutcliffe'. Display the content of the list on the screen. Then add a new band member, whose name is Pete Best. Display the content of the list on the screen again.

Do the task See the solution

Task 2

Stuart Sutcliffe left the band before the first album was released (he died shortly afterward). Remove the appropriate element from the the_beatles list. Display the new band's line-up on the screen.

Do the task See the solution

Task 3

Soon afterward, three musicians, persuaded by the manager, decided to fire the drummer Pete Best and hire Ringo Starr to replace him. Replace the appropriate element on the the_beatles list. Display the new band's line-up on the screen.

Do the task See the solution

Task 4

FInally, display the band's line-up on the screen, but do not display the entire list. Instead, display all the members individually, each in a new line.

Do the task See the solution

Next chapter: List Operations »