02.03 · Unpack and edit lists / Watch · 17 min
Change a list and keep track of returned values
Some methods change the list and return None. pop changes it and also gives back the removed item.
Activity 3 of 4 in Unpack and edit lists
Python 3: Lists / 02.03
Add and remove items
Not started
Choose append, insert, pop, remove, clear or del for the change you need.
Your lesson progress
Empty: not started. Yellow: in progress. Green: complete. Marking the whole activity complete does not replace your test results. These circles save in this browser for this pilot.

ORIGINAL WALKTHROUGH · 11:13
Watch with Rob.
Pause whenever you want to try a change. You can return to the lesson at any time.
You are signed in, but video access needs attention. Check your account access. Your saved work is kept.
Video walkthroughs are included with an active Code Zone membership. The written lesson and practice are free.
Open video & code editor (opens in a new tab)Append at the end; insert before an index
append(value) adds one item at the end. insert(index, value) inserts before that index and shifts later positions. To insert in the second position, use index 1.
These methods change the original list and return None. Call fruits.append("cherry") on its own line; do not replace fruits with the method’s return value.
fruits = ["apple", "banana"]
fruits.append("cherry")
print(fruits)
fruits.insert(1, "orange")
print(fruits)Choose by position or by value
pop() removes and returns the last item; pop(0) removes and returns the first. Save that returned value if you need it. Calling pop on an empty list raises IndexError.
remove("banana") removes the first equal value, not an index. It returns None and raises ValueError if no equal value exists. Duplicates after the first match remain.
fruits = ["apple", "banana", "cherry", "banana"]
removed = fruits.pop()
print(removed)
fruits.remove("banana")
print(fruits)An empty list is different from an absent name
clear() empties the existing list; the name still refers to it. del items removes the name binding. Trying to read that deleted name raises NameError. del items[1] instead removes one item, and del items[1:3] removes a slice.
Deleting a name does not guarantee that the underlying object is immediately destroyed. Another name may still refer to the same list. In this example, both names refer to one list, so clearing through one affects the other.
items = ["pen", "book"]
other_name = items
items.clear()
print(items) # []
print(other_name) # []
del items
print(other_name) # []Build it one step at a time
Prefer learning by typing? Work through short instructions, run the Python code and check each change before continuing. Your guided project saves separately from the challenge below.
The mini-lesson opens in a new tab. Your own code carries forward; each step has a focused check and an optional hint.
Start mini-lesson(opens in a new tab)5 steps · Saved work · Instant feedback
YOUR TURN
Make each list change explicit
Append Miami; insert Paris second. Pop and save 60; remove 30. Clear items, re-create its five original values, then delete the name. Keep the supplied verification and print order.
['London', 'Tokyo', 'Rome', 'Oslo', 'Miami'] ['London', 'Paris', 'Tokyo', 'Rome', 'Oslo', 'Miami'] 60 [10, 20, 30, 40, 50] [10, 20, 40, 50] [] False
Keep the starting inputs. Print these lines in this order, without extra output.
- Store the requested cities
- Store the requested numbers
- Store the requested removed number
- Store the requested items still defined
- Output 1 matches the requested checkpoint
- Output 2 matches the requested checkpoint
- Output 3 matches the requested checkpoint
- Output 4 matches the requested checkpoint
- Output 5 matches the requested checkpoint
- Output 6 matches the requested checkpoint
- Output 7 matches the requested checkpoint
Edit Python, then run it. Check challenge checks your output and the listed Python requirements. Your code saves on this browser after a short pause. Sign in and choose Save account draft to keep it across devices. Word wrap is enabled.
Ready to checkRun the checks to see your results here.
Your check results
Need a nudge?
Changing and returning are different
Call append and insert without replacing cities. Assign the result of numbers.pop() to removed_number.
Clear, rebuild, then delete the name
items.clear() leaves []. Reassign the five starting values, then use del items. The supplied globals check reports whether the name remains.
One possible solution
Your code may look different and still be correct. Compare the approach with your own.
Each print captures one moment in the program. Later mutations do not change earlier output, and deleting a name differs from emptying its collection.
Walk through the fix.
Choose Next to inspect one change at a time. Highlighted lines show what to notice. Your editor stays unchanged unless you choose to load a step.
Step 1 of 2
Read the original inputs
Keep the supplied inputs so each check has the same starting point.
cities = ["London", "Tokyo", "Rome", "Oslo"]Expected output for this step
(No output)
Trace each assignment and change
Each print captures one moment in the program. Later mutations do not change earlier output, and deleting a name differs from emptying its collection.
cities = ["London", "Tokyo", "Rome", "Oslo"]
cities.append("Miami")
print(cities)
cities.insert(1, "Paris")
print(cities)
numbers = [10, 20, 30, 40, 50, 60]
removed_number = numbers.pop()
print(removed_number)
print(numbers)
numbers.remove(30)
print(numbers)
items = ["pen", "book", "lamp", "cup", "key"]
items.clear()
print(items)
items = ["pen", "book", "lamp", "cup", "key"]
del items
# Supplied verification: leave these final lines unchanged.
items_still_defined = "items" in globals()
print(items_still_defined)Expected output for this step
['London', 'Tokyo', 'Rome', 'Oslo', 'Miami'] ['London', 'Paris', 'Tokyo', 'Rome', 'Oslo', 'Miami'] 60 [10, 20, 30, 40, 50] [10, 20, 40, 50] [] False
See the complete solution
cities = ["London", "Tokyo", "Rome", "Oslo"]
cities.append("Miami")
print(cities)
cities.insert(1, "Paris")
print(cities)
numbers = [10, 20, 30, 40, 50, 60]
removed_number = numbers.pop()
print(removed_number)
print(numbers)
numbers.remove(30)
print(numbers)
items = ["pen", "book", "lamp", "cup", "key"]
items.clear()
print(items)
items = ["pen", "book", "lamp", "cup", "key"]
del items
# Supplied verification: leave these final lines unchanged.
items_still_defined = "items" in globals()
print(items_still_defined)Ready to try it?
Practice in your browser and see your results.
Progress details
Have a question?
Ask the Code Zone community.
Ask a question in The Code Zone (opens in a new tab)