Lists: Many Items, Plus Indexing and Append

Python for Absolute Beginners · Lesson 12

What you’ll learn

1 / 15

One variable, many values

2 / 15

Make a list

fruits = ["apple", "banana", "cherry"]
print(fruits)

['apple', 'banana', 'cherry']

Python prints the whole list back, brackets and all, so you can see exactly what is inside.

3 / 15

Reading items by position

4 / 15

Read by position

fruits = ["apple", "banana", "cherry"]
print(fruits[0])
print(fruits[1])
print(fruits[2])

apple
banana
cherry

The same bracket notation you use to make the list, now with a position inside.

5 / 15

The last item shortcut

fruits = ["apple", "banana", "cherry"]
print(fruits[-1])

cherry

Minus 1 is a handy shortcut for the last item.

6 / 15

Counting with len

fruits = ["apple", "banana", "cherry"]
print(len(fruits))

3

len() counts a list's items, exactly as it counts a string's characters.

7 / 15

Changing a list: append and replace

8 / 15

Append and replace

fruits = ["apple", "banana", "cherry"]
fruits.append("orange")
print(fruits)
fruits[1] = "blueberry"
print(fruits)

['apple', 'banana', 'cherry', 'orange']
['apple', 'blueberry', 'cherry', 'orange']

Read the second output carefully: only position 1 changed.

9 / 15

Looping through a list

10 / 15

Loop through a list

fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
    print("I like " + fruit)

I like apple
I like banana
I like cherry

The loop adapts to the list, however many items it holds.

11 / 15

Hands-on exercise

12 / 15

Key takeaways

13 / 15

Take the quiz

Take the Lesson 12 quiz — 5 quick questions to check what you learned.

14 / 15

Next lesson

Dictionaries: Labeled Storage

15 / 15

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.