sodcalculator

10 Tech How-To

Python List pop() Method: How to Use It

Guide 10-56831 · Updated · Step by step

The pop() method removes an item from a Python list at a given index and returns that item. It is the standard way to remove and retrieve an element in one step, and it also lets you use a list as a stack (last in, first out). You only need a Python interpreter (version 3.x works fine) and a list to practice on.

What You’ll Need

  • Python 3 installed on your computer (or an online interpreter like Replit or Python.org’s shell).
  • A text editor or IDE (optional, but helpful for writing longer scripts).
  • A basic understanding of list indexing (indexes start at 0).

Step-by-Step

  1. Create a list to work with. For example: fruits = ['apple', 'banana', 'cherry', 'date']. This gives you four items with indexes 0 through 3.
  2. Call pop() with no arguments to remove the last item. Type last = fruits.pop(). This removes 'date' from the list and assigns it to the variable last. The list now becomes ['apple', 'banana', 'cherry'].
  3. Call pop(index) to remove a specific item. For example, removed = fruits.pop(1) removes the item at index 1, which is 'banana'. The list becomes ['apple', 'cherry'], and removed holds 'banana'.
  4. Use a negative index to remove from the end. fruits.pop(-1) removes the last item, just like pop() without arguments. In our example, it removes 'cherry', leaving ['apple'].
  5. Store the popped value in a variable. This is the main advantage of pop() over del – you get the value back. For instance: item = fruits.pop(0) gives you 'apple' and leaves the list empty.
  6. Use pop() in a loop to process items one by one. A common pattern is while fruits: print(fruits.pop()). This prints each item from last to first, emptying the list.
  7. Combine with append() to implement a stack. Use stack.append(item) to push and stack.pop() to pop. This gives you LIFO (last in, first out) behavior.

Common Mistakes

  • Using an index that is out of range. If you call pop(5) on a list with only 3 elements, Python raises an IndexError. Always check the list length first if you are unsure.
  • Forgetting that pop() modifies the list in place. It does not return a new list; it changes the original. If you try new_list = my_list.pop(), new_list will be the popped item, not the remaining list.
  • Confusing pop() with remove(). pop() removes by index, while remove() removes by value. Using pop('apple') will raise a TypeError because pop() expects an integer index, not a value.
  • Using pop() on an empty list. Calling pop() on a list with no elements raises an IndexError. Always check if the list is empty before popping in a loop.

Tips

  • Use pop() when you need the removed item. If you don’t need the value, use del list[index] instead – it’s slightly faster because it doesn’t return anything.
  • Remember that pop() is O(1) for the last element, but O(n) for the first. Removing from the beginning requires shifting all remaining elements. If you frequently remove from the front, consider using collections.deque instead.
  • Use negative indexes to avoid calculating the length. pop(-1) is the same as pop(len(list)-1), but shorter and clearer.
  • In a loop, iterate over a copy of the list if you need to pop while iterating. For example: for item in my_list[:]: if condition: my_list.pop(my_list.index(item)). Popping from the list you are iterating over can skip elements.
  • Use try/except to handle the case of an empty list gracefully. Wrap your pop() call in a try block and catch IndexError to avoid crashing.

Troubleshooting

IndexError: pop index out of range – This means the index you provided is either negative beyond the list’s length or larger than the last index. Check the list length with len(my_list) and verify your index is within -len(my_list) to len(my_list)-1. If you are popping in a loop, the list shrinks, so adjust your index accordingly or pop from the end.

TypeError: ‘str’ object cannot be interpreted as an integer – You passed a string to pop(). Remember that pop() expects an integer index, not a value. If you want to remove by value, use remove().

List becomes empty unexpectedly – If you are popping in a loop without checking the length, you might pop more items than intended. Use a while loop with a condition like while my_list: to stop when the list is empty.

Changes not reflected outside a function – If you call pop() inside a function and expect the original list to change, remember that lists are mutable and passed by reference. The change will be visible outside the function, but if you reassign the list inside the function (e.g., my_list = my_list.pop()), that creates a new local variable. Just call my_list.pop() without reassigning.

Frequently Asked Questions

What does pop() return?

It returns the item that was removed from the list. If you call pop() without an index, it returns the last item. If you provide an index, it returns the item at that index.

Can I use pop() on a tuple?

No. Tuples are immutable, so they don’t have a pop() method. If you need to remove items, convert the tuple to a list first, or use slicing to create a new tuple.

What’s the difference between pop() and del?

del removes an item or slice but does not return the removed value. pop() removes a single item and returns it. Use pop() when you need the value; use del when you don’t.

Does pop() work on dictionaries or sets?

Dictionaries have a pop() method that removes a key and returns its value, but it works differently (you must specify the key). Sets have pop() that removes and returns an arbitrary element, but it’s rarely used. This guide focuses on lists.

How do I remove the first item and get it?

Use my_list.pop(0). This removes the item at index 0 and returns it. Note that this is O(n) because all other elements shift left.

Can I pop multiple items at once?

No, pop() only removes one item per call. To remove multiple items, you can use a loop or slicing (e.g., del my_list[0:2]), but slicing does not return the removed items.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →