thecodezone.
The Code Zone
Short steps, at your pace · Free lessons and practice · Member video walkthroughs
Save your next small win.

Checking your account…

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

All courses

Python 3: Lists / 02.03

Add and remove items

Not started

Choose append, insert, pop, remove, clear or del for the change you need.

More options

Your lesson progress

Walkthrough: not startedPractice: not started

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.

Rob appends cherry after apple and banana, then pops cherry into a separate returned-value box.
Rob’s comic strip · Unpack and edit lists

ORIGINAL WALKTHROUGH · 11:13

Watch with Rob.

Pause whenever you want to try a change. You can return to the lesson at any time.

LEARN WITH THE CODE ZONE

Unlock this video walkthrough

Written lessons and coding practice are free. Video walkthroughs are included with a paid Code Zone membership or an active free trial.

Have a Code Zone website account? Use your learning account’s email to get a six-digit sign-in code, or use your existing website password.

Need a website account? Paid Skool members and active trial members can use the email from their membership questions to request a sign-in code. No invite link or Skool password needed.

Close this window to keep practicing. After signing in, you’ll return to this lesson.

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)

Starter source code for VS Code

Copy the source below or download the file to follow along in VS Code.

The download includes a lesson link in each file and a README so you can find your way back.

main.py
main.pypython · example
# Code along with Rob: 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.
# Predict each result, then press play.

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.

Append at the end; insert before an index
lists.pypython · example
fruits = ["apple", "banana"]
fruits.append("cherry")
print(fruits)
fruits.insert(1, "orange")
print(fruits)
Open workspace to paste this example (opens in a new tab)

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.

Choose by position or by value
lists.pypython · example
fruits = ["apple", "banana", "cherry", "banana"]
removed = fruits.pop()
print(removed)
fruits.remove("banana")
print(fruits)
Open workspace to paste this example (opens in a new tab)

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.

An empty list is different from an absent name
lists.pypython · example
items = ["pen", "book"]
other_name = items
items.clear()
print(items)       # []
print(other_name)  # []
del items
print(other_name)  # []
Open workspace to paste this example (opens in a new tab)

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.

Expected output
['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.

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.

Replace your editor code?

Your current work will be replaced. Choose Cancel to keep it.

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)

Continue learning

ONE STEP FORWARD

Choose the operation by what changes and what it returns. Keep pop’s returned item separate from the remaining list.