Start learning free

LESSON 103 OF 410 · LISTS I

Remove Last Item From List in Python

Lesson Read the full explanation

Another way to change the data in a list is with the .pop() method.

.pop() removes the last element of a list and also returns the value of the removed element:

threeArr = [1, 4, 6]

# Remove last element:
oneDown = threeArr.pop()

print(oneDown) # Output: 6
print(threeArr) # Output: [1, 4]

# Remove last element again:
oneDown = threeArr.pop()

print(oneDown) # Output: 4
print(threeArr) # Output: [1]

Assignment
Follow the Coding Tutorial and let's play with some arrays.


Hint
Look at the examples above if you get stuck.


STEP 1 OF 2
    Ctrl + Enter to check

    PYTHON · LISTS I

    Array Pop in Python, explained

    Python list.pop(): Remove and Retrieve the Last Element

    When you need to remove an item from the end of a list — and optionally use its value — .pop() is your tool. It does two things in one step: removes the last element from the list and returns it so you can work with it. This makes it especially powerful for implementing stacks, processing queues in reverse, or undoing the most recent action in an app.

    How .pop() Works

    stack = [10, 20, 30, 40]
    
    # Removes and returns the last element:
    removed = stack.pop()
    
    print(removed)   # 40
    print(stack)     # [10, 20, 30]
    
    # Call it again:
    removed = stack.pop()
    print(removed)   # 30
    print(stack)     # [10, 20]
    

    Notice that .pop() modifies the original list. Each call shrinks the list by one.

    Popping by Index

    .pop() also accepts an optional index. Pass an index to remove (and return) any element, not just the last:

    fruits = ["apple", "banana", "cherry", "date"]
    
    # Remove the item at index 1:
    removed = fruits.pop(1)
    print(removed)   # banana
    print(fruits)    # ['apple', 'cherry', 'date']
    
    # No argument = last item:
    fruits.pop()
    print(fruits)    # ['apple', 'cherry']
    

    Real-World Example: Implementing an Undo Stack

    One of the most classic uses of .pop() is building an undo system — every action gets pushed onto a stack, and undoing removes the most recent one:

    actions = []
    
    # User performs actions:
    actions.append("typed 'Hello'")
    actions.append("bold text")
    actions.append("changed font size")
    
    print("Current state:", actions)
    
    # User hits Ctrl+Z (undo):
    last_action = actions.pop()
    print("Undid:", last_action)       # Undid: changed font size
    print("Current state:", actions)   # ['typed \'Hello\'', 'bold text']
    

    pop() vs. remove(): What's the Difference?

    • list.pop(i) — removes by index, returns the removed item
    • list.remove(x) — removes by value (first match), returns None
    nums = [5, 10, 15, 10, 20]
    
    # remove() — finds value 10 and removes the first occurrence:
    nums.remove(10)
    print(nums)   # [5, 15, 10, 20]
    
    # pop() — removes by position:
    nums.pop(0)
    print(nums)   # [15, 10, 20]
    

    Common Mistakes to Avoid

    • Calling .pop() on an empty list: Raises IndexError. Always check if my_list: first.
    • Ignoring the return value when you need it: list.pop() returns the removed element — if you just write my_list.pop() without capturing the result, the value is gone.
    • Popping with an out-of-range index: my_list.pop(99) on a 3-item list raises IndexError.
    queue = []
    
    # Safe pattern — check before popping:
    if queue:
        item = queue.pop()
    else:
        print("Nothing to remove — list is empty")
    

    Key Takeaways

    • list.pop() removes and returns the last item; list.pop(i) removes and returns item at index i.
    • It modifies the list in place — the removed item is gone from the list.
    • Always guard against IndexError by checking the list isn't empty first.
    • Use it for stacks (LIFO), undo systems, or any time you need to consume items from the end of a list.

    What to Learn Next

    With append() and pop() in your toolkit, you now have a fully functional stack data structure. Explore how stacks are used to solve real algorithm problems — like validating parentheses or reversing a string — and practice using len() to safely check list size before popping.

    Keep going with PythonAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free