list.pop()

list.pop([i])
Returns: any · Updated March 13, 2026 · List Methods
lists methods mutation

The .pop() method removes and returns the element at the specified index in a list. By default, it removes and returns the last element. This is one of the fundamental list operations, commonly used for implementing stacks and accessing/removing elements.

Syntax

list.pop([i])

Parameters

ParameterTypeDefaultDescription
iint-1 (last)The index of the element to remove. If omitted, removes and returns the last element.

Examples

Basic usage - removing the last element

Remove and return the last element:

fruits = ["apple", "banana", "cherry"]
last = fruits.pop()
print(last)
# 'cherry'
print(fruits)
# ['apple', 'banana']

Removing from a specific index

Remove and return an element at a specific position:

colors = ["red", "green", "blue", "yellow"]
middle = colors.pop(1)
print(middle)
# 'green'
print(colors)
# ['red', 'blue', 'yellow']

Using pop with negative indices

Python supports negative indexing:

queue = ["first", "second", "third"]
last = queue.pop(-1)
print(last)
# 'third'
second_last = queue.pop(-1)
print(second_last)
# 'second'

The IndexError pitfall

Calling .pop() on an empty list raises an IndexError:

empty = []
# empty.pop()
# IndexError: pop from empty list

# Safe pattern: check length first
if items:
    item = items.pop()

Common Patterns

Stack implementation

Lists with .append() and .pop() naturally implement a LIFO stack:

stack = []
stack.append(1)      # push
stack.append(2)
stack.append(3)

while stack:
    item = stack.pop()  # pop
    print(item)
# Output: 3, 2, 1

Removing items during iteration

A common pattern involves removing items that meet a condition:

numbers = [1, 2, 3, 4, 5, 6]
while numbers:
    num = numbers.pop()
    if num % 2 == 0:
        print(f"Removed even: {num}")

print(numbers)
# [1, 3, 5]

Important: Never modify a list while iterating over it with a range loop. Use .pop() with a while loop or list comprehension instead.

Processing with known count

When you need to process a specific number of items:

tasks = ["task1", "task2", "task3", "task4", "task5"]
for _ in range(min(3, len(tasks))):
    task = tasks.pop()
    print(f"Processing: {task}")

print(f"Remaining: {tasks}")
# Processing: task5
# Processing: task4
# Processing: task3
# Remaining: ['task1', 'task2']

Getting the index while popping

If you need both the value and its index, use enumerate or calculate manually:

items = ["a", "b", "c", "d"]
index = 2
value = items.pop(index)
print(f"Removed {value} at index {index}")
# Removed c at index 2

Performance Note

  • .pop() without an index (or with -1) is O(1) — constant time
  • .pop(0) (from the front) is O(n) — requires shifting all remaining elements
  • For frequent front removals, use collections.deque instead
from collections import deque

queue = deque(["a", "b", "c"])
queue.popleft()  # O(1) operation

See Also