Drop hier links of afbeeldingen om ze aan de editor toe te voegen.

A list is changeable. Once it exists you can replace any item in it, add new items to it and take items out of it again, all without creating a second list. That is most of what makes a list more useful than a row of separate variables.

Every example below starts from this list:

countries = ["UK", "USA", "Chad", "Australia", "Thailand"]

Replacing one item

An item is replaced by assigning to its index. The list name and the index go on the left of the =, and the new data goes on the right:

countries[3] = "Mexico"

That replaces Australia, the item at index 3, with Mexico. The list still has five items and every other item is untouched.

Three parts again, and each of them matters:

The old data is gone. Assigning to an index does not push anything sideways or make the list longer, it overwrites one position.

The new data can be worked out from what is already in the list. This puts UK together with ingdom, so that countries[0] ends up holding UKingdom:

countries[0] = countries[0] + "ingdom"

An item can also be copied from one position to another:

countries[1] = countries[4]

Both positions now hold Thailand. A list is allowed to contain the same item twice.

Doing sums with items in a list

A list of numbers can be used in a calculation anywhere a number could be used. These examples start from:

prime_numbers = [1, 3, 5, 7, 11, 13]

Adding one to the first item in the list:

prime_numbers[0] += 1

Multiplying the second and third items and storing the answer in an ordinary variable, which then holds 15:

total = prime_numbers[1] * prime_numbers[2]

Subtracting the fourth item from the fifth and storing the answer back into the list, so that prime_numbers[0] ends up holding 4:

prime_numbers[0] = prime_numbers[4] - prime_numbers[3]

Adding an item

There are two ways to make a list longer, and the difference between them is where the new item lands.

append puts the new item on the end of the list:

countries.append("Peru")

insert puts it at a position you choose. The index comes first, then a comma, then the item:

countries.insert(1, "Peru")

Peru becomes the item at index 1, and everything from the old index 1 onwards shifts one place to the right. Nothing is overwritten: the list grows by one item either way.

countries.insert(1, "Peru") and countries[1] = "Peru" look similar and do different things. insert adds an item and makes the list longer. Assigning to countries[1] replaces the item that is already there and leaves the list the same length.

Removing an item

There are two ways to make a list shorter, and here the difference is how you say which item to take out.

remove searches the list for a piece of data and takes it out:

countries.remove("Chad")

pop works by position instead. With an index in the brackets it removes the item at that index:

countries.pop(2)

With empty brackets it removes the last item, whatever that happens to be:

countries.pop()

Either way the list gets one item shorter and everything after the gap shifts one place to the left, so the indices of the remaining items change.

remove needs to know what to take out, so it is given the data: countries.remove("Chad").

pop needs to know where to take something from, so it is given an index, or nothing at all if you mean the end: countries.pop(2) or countries.pop().

How many items are in the list?

len answers that:

print(len(countries))

For the five-country list that prints 5. It is worth remembering that the largest valid index is len(countries) - 1, because the counting starts at zero. That is the check a program makes when it has to decide whether an index the user typed is usable.

Is an item in the list?

Python has a keyword for this. in is a condition, so it produces True or False and fits straight into an if:

if "Chad" in countries:
    print("Chad is in the list")
else:
    print("Chad is not in the list")

The match is exact, the way == is exact: "chad" is not "Chad", and neither is "Chad " with a stray space on the end.

Try it yourself

Predict what each list looks like at the end, then run the code and compare.

countries = ["UK", "USA", "Chad", "Australia", "Thailand"]

countries[3] = "Mexico"
countries[0] = "Iceland"
countries[1] = countries[4]

print(countries)
square_numbers = [1, 4, 9, 16, 25, 36]

square_numbers[5] = 49
square_numbers[0] += 1
total = square_numbers[3] - square_numbers[1]

print(square_numbers)
print(total)
food = ["bacon", "cheese", "pasta", "beans"]

food.append("tomatoes")
food.insert(1, "ice cream")
food.remove("cheese")
food.pop()

print(food)
print(len(food))

The last one is worth tracing line by line. Two items go in and two come out, so the list is back to four items, but they are not the four it started with. Watch the last line in particular: pop() takes off whatever is at the end of the list at that moment, and by then that is the tomatoes that append put there four lines earlier.

Help! My code doesn't work!

Make sure that you check for the following things:

  • The list name is spelled identically everywhere it is used, capital letters included.
  • The index is surrounded by square brackets.
  • List indexing starts at zero. Are you counting properly?
  • append, insert, remove and pop are written after the list name with a full stop between them, as in countries.append("Peru").
  • insert takes the index first and then the item.
  • remove is given the item itself, and pop is given an index or nothing at all.