Editor

Fix the Bug: IndexError

Beginner · Fix the bug · from Python Lists: Create, Access, Modify, and Loop Through Lists

From the lesson: Python Lists: Create, Access, Modify, and Loop Through Lists

Think about your favorite music playlist. It has a bunch of songs stored in a specific order. You can add new songs, remove old ones, rearrange them, or skip to song number five.

Read the lesson

This code tries to print the last color in the list, but it crashes with an IndexError. Fix the bug so it prints:

purple

Hint: The list has 4 items. What are the valid indexes?

colors = ["red", "green", "blue", "purple"]
print(colors[4])

More Lists exercises

All Python exercises · 50 practice problems · Data Structures quiz