✦ Python Foundations Collections module

Concept 08 · List Operations & Methods

Change a list
with clear operations.

Lists can grow, shrink, and change as your program works with a group of values. Learn to choose an operation by what you know: a value or a position.

01 Learn02 Try03 Predict

Learning as a guest

Learning as a guest? Save your progress!

Create a free account to learn with Sloby AI, check your understanding, and track your progress.

The entire platform is free throughout 2026. Foundations stays free forever.

List operations intentionally change list state.

You already know that a list stores ordered items and that each item has an index. Now you will choose operations that deliberately change its contents: list before → operation → list after. A list is <strong>mutable</strong>, so an operation can change the existing list instead of making you start over.

tasks = ["Learn Python", "Practice loops"]
tasks.append("Build a project")
print(tasks)

Read this as a state change: ["Learn Python", "Practice loops"] → append("Build a project") → ["Learn Python", "Practice loops", "Build a project"]. append(value) adds one item at the end; the earlier items keep their order, and the same list changes.

['Learn Python', 'Practice loops', 'Build a project']

Ask one useful question before you choose a method: “Do I know the value, or do I know the position?” remove(value) looks for a matching value. pop(index) removes the item at a position and returns that removed value.

↳

A useful decision: remove(value) asks “Which value?” pop(index) asks “Which position?” The number in remove(1) is a value to find; the number in pop(1) is index 1.

Remove a value, then keep a removed item
items = ["book", "pen", "bag"]
items.remove("pen")
removed = items.pop(1)
print(items)
print(removed)

remove("pen") removes by value. The list becomes ["book", "bag"], so index 1 now refers to "bag". Then pop(1) removes "bag" and stores that returned value in removed. Indexes belong to the list’s current state and can change after insert(), remove(), or pop().

Insert a new item without replacing one
items = ["book", "bag"]

items.insert(1, "pen")

print(items)

insert(index, value) puts a new item at that position and shifts later items right: ["book", "bag"] → ["book", "pen", "bag"]. This differs from items[index] = value, which replaces the existing item instead of adding a new one.

Remove by value or remove by position?
items = [1, "one", 2]

items.remove(1)
removed = items.pop(1)

print(items)
print(removed)

remove(1) finds the value 1; it does not mean “remove index 1.” After that removal, pop(1) removes the current item at index 1 and returns it. Use pop() with no argument when you want to remove and keep the final item.

Count items and check before removing
tasks = ["Learn Python", "Practice loops"]

print(len(tasks))
print("Learn Python" in tasks)

if "Practice loops" in tasks:
    tasks.remove("Practice loops")

len(tasks) asks “How many items are in the list?” Its last valid index would be len(tasks) - 1. The expression value in list gives a Boolean result, connecting lists to bool and if. Checking first is useful because remove() can raise ValueError when the value is absent.

Trace the list as its state changes
tasks = ["learn", "practice"]

tasks.append("build")
tasks.insert(1, "review")
tasks.remove("practice")
last_task = tasks.pop()

print(tasks)
print(last_task)
print(len(tasks))

Pause before running it. Predict the final list, the value in last_task, and len(tasks). The list changes one operation at a time: ["learn", "practice"] → ["learn", "practice", "build"] → ["learn", "review", "practice", "build"] → ["learn", "review", "build"] → ["learn", "review"]. last_task is "build", and the final length is 2.

Choose an operation by your intention

  1. Add at the endUse append(value) when the new item belongs after the current final item.
  2. Add at a positionUse insert(index, value) when the new item belongs before the item currently at that index.
  3. Remove deliberatelyUse remove(value) when you know the value; use pop(index) when you know the position; use pop() for the final item.
  4. Check the current stateUse len(list) to count items and value in list to check whether a value is present before removing it.

Common beginner mistakes

  • Confusing a value with a positionremove(1) looks for the value 1; pop(1) removes the item at index 1.
  • Removing a value that is not presentitems.remove("missing") can raise ValueError. Use if "missing" in items: when the value might be absent.
  • Forgetting that indexes changeAfter insert(), remove(), or pop(), an old index may no longer point to the same item.
  • Forgetting that pop returns a valueremoved = items.pop() does not only shorten the list: it gives you the removed final value so you can use it later.

Python Playground

Loading Python runtime…

Before running the code, predict the list after each operation. Change one operation at a time, then compare <code>remove()</code> and <code>pop()</code>: which one uses a value, which one uses a position, and which one gives a removed value back?

list_operations.pyPython
OutputWaiting for Python
The browser runtime is loading. You can still edit the example code.

Pyodide runs Python in a browser Web Worker. It is stopped and restarted if code keeps running. Browser execution is not a complete security boundary; do not enter secrets or rely on it for untrusted code isolation.

What will this program print?

Follow the list after append, then remove the item at index 1 with pop.

numbers = [5, 10, 15]

numbers.append(20)
numbers.pop(1)

print(numbers)
Choose the output
Sloby, the Python AI Tutor mascot

Your learning mentor

Sign in to ask Sloby

Create a free account or sign in to get AI guidance while you learn.

Sign in to check your understanding

Create a free account or sign in to receive AI feedback and save your learning progress.

Python AI Tutor · Learn by understanding, not just by running code.