append and change an item by positionSo far every variable has held one value: one name, one number, one
string. But real programs deal in groups — a class of students, a
week of temperatures, a shopping list. A list stores many values
in a single variable, in order, inside square brackets, separated by
commas:
fruits = ["apple", "banana", "cherry"]
print(fruits)
Output:
['apple', 'banana', 'cherry']
Python prints the whole list back, brackets and all, so you can see
exactly what is inside. The items can be any mix of kinds — strings,
numbers, booleans — though in practice a list usually holds one kind
of thing, because that keeps the code readable.
Like string characters in Lesson 6, list items are counted from 0.
The first item is position 0, the second is position 1, and so on:
fruits = ["apple", "banana", "cherry"]
print(fruits[0])
print(fruits[1])
print(fruits[2])
Output:
apple
banana
cherry
Asking for a position past the end gives an error, just as with
strings — there is no fourth fruit in a three-item list. A handy
shortcut: position -1 means the last item, -2 the second-to-last,
and so on:
fruits = ["apple", "banana", "cherry"]
print(fruits[-1])
Output:
cherry
len() counts a list’s items, exactly as it counts a string’s
characters:
fruits = ["apple", "banana", "cherry"]
print(len(fruits))
Output:
3
Lists are changeable — unlike strings, you can alter them after
making them. The two changes beginners use most are adding to the end
and replacing one item:
fruits = ["apple", "banana", "cherry"]
fruits.append("orange")
print(fruits)
fruits[1] = "blueberry"
print(fruits)
Output:
['apple', 'banana', 'cherry', 'orange']
['apple', 'blueberry', 'cherry', 'orange']
append() sticks a new item on the end. fruits[1] = "blueberry"
replaces whatever was at position 1 — the same bracket notation you
use to read an item, now on the left of the = sign, which means
“store” (Lesson 4). Read the second output carefully: the order of
the other items did not change; only position 1 did.
Lists and loops (Lesson 9) belong together. A for loop walks a list
one item at a time, so you can do something with every item without
knowing in advance how many there are:
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print("I like " + fruit)
Output:
I like apple
I like banana
I like cherry
The loop variable fruit takes each value in turn: first "apple",
then "banana", then "cherry". If you later append "orange" to
the list, the same loop prints four lines with no other change. That
is the real power of lists: code that works for three items works for
three hundred, because the loop adapts to the list.
Press play on any slide and the lesson plays on by itself from there. You can still click Next or click a slide to move on.
Download the Lesson 12 slides (PowerPoint, with narration)
Download the Lesson 12 slides (PDF)
len().A list holds many items in order. append adds one item at the end and len counts the items. Change the fruit you add and run again.
Try this: Change the fruit to mango and press Run again — mango appears at the end of the list.
Press Run to see the output here.
Save this as lesson12-practice.py and run it with F5 in IDLE.
# Lesson 12: Lists — Many Items, Indexing, Append
# WHAT TO DO: A list stores many values in order, inside square
# brackets. Positions start at 0. This list of favorite foods has one
# placeholder item. Fill in line 11: replace TODO with a real food
# of your own, keeping it inside quotes.
# HINT: Only change the word TODO. The rest of the program already
# prints the list, its first item, how many items it has, adds a fifth
# food with append(), and prints "I like ..." for every item.
# Run with F5 in IDLE. You are done when no TODO appears in the output.
foods = ["pizza", "TODO", "pasta", "salad"] # <-- CHANGE THIS LINE
print(foods)
print(foods[0])
print(len(foods))
foods.append("soup")
print(foods)
for food in foods:
print("I like " + food)
-1 is the last item; len() counts items.append() adds to the end, list[i] = ... replaces one item.for loop over a list handles every item, however many there are.Next lesson: Dictionaries.
["apple", "banana", "cherry"], what is at position 0, and what is at position -1?len() do to a list?fruits.append("orange") and fruits[1] = "blueberry"?Answer key: 1. Many values stored in order in a single variable, inside square brackets. 2. Position 0 is "apple"; position -1 is "cherry", the last item. 3. It counts how many items the list has. 4. append() adds a new item at the end; fruits[1] = ... replaces the item already at position 1. 5. Because the loop walks whatever items are in the list at run time, however many there are.