Python Lists
💡
Exercise 22

Grocery 10 XP Easy

Ctrl+Enter Run Ctrl+S Save

Welcome to Chapter 5: Lists! 📝

💡 Think of it like this: A list is like a shopping list — it's an ordered collection of items. You can add items, remove items, change items, and look at specific items by their position.

# Creating a list fruits = ['apple', 'banana', 'cherry'] # Accessing items (index starts at 0!) print(fruits[0]) # apple print(fruits[1]) # banana print(fruits[-1]) # cherry (last item)

Common list operations:

  • .append(item) — Add an item to the end
  • .remove(item) — Remove a specific item
  • .pop() — Remove and return the last item
  • .sort() — Sort the list in order
  • len(list) — Get how many items are in the list
grocery = ['milk', 'eggs'] grocery.append('bread') # ['milk', 'eggs', 'bread'] grocery.remove('eggs') # ['milk', 'bread'] print(len(grocery)) # 2
📋 Instructions
Create a grocery list with 5 items: ```python grocery = ['eggs', 'milk', 'bread', 'rice', 'butter'] ``` Then print: 1. The entire list 2. The first item 3. The last item 4. The length of the list Expected output: ``` ['eggs', 'milk', 'bread', 'rice', 'butter'] eggs butter 5 ```
Use grocery[-1] or grocery[4] for the last item. Use len(grocery) for the count.
⚠️ Try solving it yourself first — you'll learn more!
# Creating a list
fruits = ['apple', 'banana', 'cherry']

# Accessing items (index starts at 0!)
print(fruits[0])   # apple
print(fruits[1])   # banana
print(fruits[-1])  # cherry (last item)
🧪 Test Cases
Input
Run your code
Expected
['eggs', 'milk', 'bread', 'rice', 'butter'] eggs butter 5
Expected program output
main.py
Hi! I'm Rex 👋
Output
Ready. Press ▶ Run or Ctrl+Enter.