1
Hatchling0 / 300 XP
0day streak 0stars
Your Python practice
Mock tests Readiness
More
Guest mode · saved on this device.

Lists

A list holds many values in order, inside square brackets:

fruits = ["apple", "banana", "cherry"]
0"apple"-3
1"banana"-2
2"cherry"-1
  • fruits[0] reads an item (indexes start at 0, like strings)
  • fruits[1] = "kiwi" replaces an item, because lists are mutable
  • fruits.append("date") adds to the end
  • len(fruits) counts items

See it run

fruits = ["fig", "kiwi", "lime"]
fruits.append("plum")
fruits[0] = "pear"
print(fruits[-1], len(fruits))

One list, changed in place. Watch the same box grow and change.

Practice lists

Exercise 1

Start with fruits = ["apple", "banana", "cherry"]. Append "date", then replace "banana" with "blueberry" by index. Print len(fruits).

Exercise 2

Use the playlist in the editor below. The radio host wants to:

  • append "Outro" to the end
  • replace the first song with "Sunrise" by index
  • set first to the first song and last to the last song (use a negative index), after the changes
  • set count to the number of songs

Exercise 3

In the line list in the editor below, the first and last people want to swap places. Swap line[0] and line[-1] using indexes (don't retype the names), then append "Eve", who just arrived. The result should be ["Dee", "Ben", "Cy", "Ana", "Eve"].

The explanation and tasks are public. Running this lesson requires an account with course access.

Practice

Loading Python…
More
Loading exercise content…
Visualizer Watch your code run line by line

Run your code, then choose Play to follow its execution.

Memory · names → values
Variables appear here as Python creates them.
Output & plots

One-line daily quest