YOUR SAVED CODE-ALONG
Code along: Add and remove items
Keep the lesson open on your other screen. Your edits stay in this workspace, separate from the practice activity. Sign in to save and reload a private account draft across devices, or share a snapshot when you need help.
CODE-ALONG / THEATER MODE
Add and remove items
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.
Video settings & progress
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.
Start the video to enable typing controls.
YOUR TURN
Code along: Add and remove items
Compare append and insert. Save a value returned by pop; remove an item by value. Compare clearing a list with deleting its name, including an alias. Pause the original recording to type and run your examples. This open workspace saves separately from scored practice and has no fixed-answer checks.
Load an example, edit it and run it. Word wrap is enabled.
Your check results
Lesson reference
Examples load into this same code-along only after you confirm replacing your code.
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
See the attached lesson’s comic
