Skip to content
Lesson 1 of 9 Free Preview
Duration: 20 min

Lists

Learn it

A list holds many values in order: snacks = ["apple", "popcorn"]. Use append() to add, remove() to remove, len() to count, and a for loop to go through them.

🧰 Tool: any free online Python editor such as Online Python or Trinket, or install Python from python.org and use IDLE.

Code examples

Python

snacks = ["apple", "popcorn", "grapes"]
snacks.append("cheese")
snacks.remove("popcorn")
print(snacks)          # ['apple', 'grapes', 'cheese']
print(len(snacks))     # 3
print(snacks[0])       # apple
print(snacks[-1])      # cheese (last item)
for s in snacks:
    print("I like", s)

Project: Shopping list app

  1. Start with an empty list: items = [].
  2. Use a while loop to ask for items until the user types ‘done’.
  3. Print the list numbered from 1.

Challenge: Sort the list alphabetically with items.sort().

Enjoying this lesson?

Enroll in the full course to access all lessons, track your progress, and earn a certificate.

Enroll in Full Course