──────────────────────────────────────

21. The Club Library · Build a Reading Queue

Read a list position and add an item to the end of the list.

Lesson files · lessons/lists-and-records/lists-and-indexes

Before you start
- Use an index to read a character from text.
- Use len() to count the words in a list.

By the end
- Use append() to put a new title at the end of a list.
- Use an index to read the first or last item.
- Explain how appending an item changes the result of len().

Where you can use it
Keep a queue of songs to practise. Append a new song and use an index to see which one is first or last.

A list keeps values in order. Start the reading list with ["Star Map", "Moon Garden"]. Use titles[0] to read the first title and titles[-1] to read the last.

The call titles.append("Sky Lab") changes the list by adding one title at the end. Now len(titles) gives 3, and titles[-1] gives Sky Lab.

Predict
Which title does titles[0] select?
- Star Map
- Sky Lab

Worked example (example.py)

titles = ["Star Map", "Moon Garden"]
titles.append("Sky Lab")
print(titles[0])
print(len(titles))


Example result
Star Map
3

Read a title, add it to ["Star Map", "Moon Garden"], and print the final title and the list length.

Starter program (starter.py)

titles = ["Star Map", "Moon Garden"]
titles.append("Sky Lab")
print(titles[0])
print(len(titles))


Hint
titles[-1] reads the last item. Use input() as the value to append.

Case 1. Try the sample
Input
Night Atlas
Expected output
Night Atlas
3

Case 2. Another title
Input
Forest Map
Expected output
Forest Map
3

Stretch
Print the first and last titles before and after adding a book.

Reflect
What index would you use for the second item?

Check your prediction
Star Map Position 0 is the first item. append() adds to the end.
