Bigger programs · lesson 1 of 12
Parts in this lesson
A[1:3]from 1.5, now for lists
Binis it in there?
Csorted() .sort()put in order
D.remove() .pop() deltake items out
E[[a, b], [c]]lists in a list

Lists in depth

Last updated

Lesson 1.9 made lists and read them one item at a time. In this lesson you'll take slices of a list, ask whether something is in it, sort it, take items out, and keep lists inside a list. Then you'll fix a loop that quietly skips items because it changes the list it's looping over.

By the end of this lesson you can

  • Take a slice of a list, and check whether a value is in it with in
  • Sort a list with sorted() or .sort(), and know which one changes the list
  • Take items out with .remove(), .pop() and del
  • Spot a loop that changes the list it's looping over, and fix it by looping over a copy

Slices of a list

In lesson 1.5 you took slices of text, like title[4:10]. Lists slice the same way: titles[1:3] is a new list of the items from position 1 up to, but not including, position 3.

Leave out the start to begin at the front, or the end to go to the last item. [-2:] is the last two items, however long the list is.

Step 1 code main.py
Edit the code, then run it again

Line 6 shows that slicing doesn't change the list: a slice is a new list. titles[:], with both numbers left out, is a copy of the whole list. You'll need that in step 6.

Is it in the list?

in asks whether a value is somewhere in a list, and gives True or False. not in asks the opposite. You met in with text in lesson 1.5; it works on every collection in this stage.

.index() gives the position of a value. Ask with in first: if the value isn't there, .index() stops the program with a ValueError.

Step 2 code main.py
Edit the code, then run it again

Sorting: sorted or .sort

There are two ways to sort, and the difference matters.

  • sorted(titles) gives back a new sorted list and leaves titles as it was (lines 2 and 3).
  • titles.sort() sorts the list itself and gives back nothing (line 4).

Both sort text alphabetically and numbers from small to large. Add reverse=True for the other way round.

Step 3 code main.py
Edit the code, then run it again

Because .sort() gives back nothing, titles = titles.sort() is a classic mistake. Run this and see what's left:

Step 3 code main.py
Edit the code, then run it again

None is Python's value for "nothing" (lesson 1.8): the list was sorted, then thrown away. Write titles.sort() on its own line, or titles = sorted(titles).

Taking items out

Three ways to take items out of a list:

WriteTakes outGives back
loans.remove("Emma")the first item equal to "Emma"nothing
loans.pop()the last itemthat item
loans.pop(0)the item at position 0that item
del loans[0]the item at position 0nothing

Watch it run and see the list shrink line by line.

Step 4 code main.py
Edit the code, then run it again

.remove() of a value that isn't in the list, or .pop() from an empty list, stops the program with an error, so check with in or len() first when you're not sure.

Lists of lists

A list can hold any value, including other lists. Here each shelf is a list of titles, and shelves is a list of shelves. A long list can be written over several lines, one item to a line.

Two positions in a row reach inside: shelves[2] is the third shelf, and shelves[2][0] is the first book on it.

Step 5 code main.py
Edit the code, then run it again

Changing a list while you loop over it

All three copies of Dune came back, so this loop should leave only "Emma" on the list. Press Run, or Watch it run to see which items the loop visits.

Step 6 code main.py
Edit the code, then run it again

There's no error here: Python did what the lines said, so the mistake is easy to miss. The rule: don't add or remove items in a list while a for loop is walking through that same list. Loop over a copy, or build a new list of the items you want to keep. Lesson 2.4 shows a short way to do that.

Check what you learned

Four questions. Pick or type an answer, then check it.

What does this program print?
titles = ["Dune", "Emma", "Beloved", "Matilda"]
print(len(titles[1:3]))
You want a sorted copy of titles, and titles itself left as it is. Which do you write?
loans = ["Dune", "Emma"], then book = loans.pop(). What is book?
Why does removing items from a list inside for item in that_list: go wrong?

Sources

Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.

All sources for Simple Python