Lesson 7 of 12

Lists 📚

🎯 Grades 6–8 ⏱ ~25 minutes 💚 Intermediate

What You'll Learn

  • Create and access lists
  • Add and remove items
  • Loop through a list

What Is a List?

A list stores multiple values in order, in one variable. Use square brackets, with items separated by commas:

Python
fruits = ["apple", "banana", "cherry"]
scores = [95, 88, 72, 100]
print(fruits)     # ["apple", "banana", "cherry"]
print(scores[0])  # 95  (index 0 = first item!)
print(fruits[2])  # cherry (index 2 = third item)
💡
Zero Indexing

Python lists start at index 0, not 1. The first item is [0], second is [1], and so on.

Adding and Removing

Python
animals = ["cat", "dog"]
animals.append("bird")   # add to end
print(animals)           # ["cat", "dog", "bird"]
animals.remove("dog")    # remove by value
print(animals)           # ["cat", "bird"]
print(len(animals))      # 2

Looping Through a List

Python
colors = ["red", "green", "blue"]
for color in colors:
    print(f"I like {color}")
# Output:
# I like red
# I like green
# I like blue
👔">Grocery List

Create a grocery list with 5 items. Print the list, then add one item and remove another. Print the final list and how many items are in it.

Quick Check

How do you access the first item of a list called 'items'?

Aitems[1]
Bitems[0]
Citems.first()

Which method adds an item to the end of a list?

Aadd()
Binsert()
Cappend()

What does len(['a','b','c']) return?

Aabc
B0
C3