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()anddel - 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.
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.
Sorting: sorted or .sort
There are two ways to sort, and the difference matters.
sorted(titles)gives back a new sorted list and leavestitlesas 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.
Because .sort() gives back nothing, titles = titles.sort() is a classic mistake. Run this and see what's left:
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:
| Write | Takes out | Gives back |
|---|---|---|
loans.remove("Emma") | the first item equal to "Emma" | nothing |
loans.pop() | the last item | that item |
loans.pop(0) | the item at position 0 | that item |
del loans[0] | the item at position 0 | nothing |
Watch it run and see the list shrink line by line.
.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.
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.
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.
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.
- The Python Tutorial: An Informal Introduction to Python, Lists: slices of a list, and lists inside lists
- The Python Tutorial: Data Structures, More on Lists: remove, pop, index and sort
- The Python Tutorial: The del statement
- Built-in Types: Common Sequence Operations: in, not in and slices
- Built-in Types: list.sort: sorts in place and returns None
- Built-in Functions: sorted
- The Python Tutorial: More Control Flow Tools, for Statements: loop over a copy when you change a collection