Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

Python Dictionary

  1. Home
  2. Python Dictionary
  3. list.pop() / list.remove() / list.clear()

list.pop() / list.remove() / list.clear()

Methods and syntax for removing elements from a list. You can remove by index, by value, or delete all elements depending on your needs.

Syntax

# Removes the element at the given index and returns it (defaults to the last element).
list.pop(index=-1)

# Removes the first element whose value matches x (returns None).
list.remove(x)

# Removes all elements from the list (returns None).
list.clear()

# Removes the element at the given index or slice range.
del list[index]
del list[start:end]

Method List

Method / SyntaxDescription
list.pop(i=-1)Removes the element at index i and returns its value. If omitted, removes and returns the last element.
list.remove(x)Removes the first occurrence of value x in the list. Raises ValueError if the value is not found.
list.clear()Removes all elements from the list, leaving it empty. Equivalent to del list[:].
del list[i]Removes the element at index i or a slice range. The removed value is not returned.

Sample Code

# Use pop() to remove the last element (stack pop operation).
stack = [1, 2, 3, 4, 5]
last = stack.pop()
print(last)   # 5
print(stack)  # [1, 2, 3, 4]

# Pass an index to pop() to remove an element at a specific position.
first = stack.pop(0)
print(first)  # 1
print(stack)  # [2, 3, 4]

# Example of a stack (LIFO) implementation.
stack = []
stack.append("Page A")
stack.append("Page B")
stack.append("Page C")
print(stack.pop())  # Page C (last in, first out)

# Use remove() to delete an element by value.
fruits = ["apple", "orange", "grape", "orange"]
fruits.remove("orange")  # Removes the first occurrence of "orange".
print(fruits)  # ['apple', 'grape', 'orange']

# Trying to remove a value that doesn't exist raises an exception.
try:
    fruits.remove("peach")  # Raises ValueError because "peach" is not in the list.
except ValueError:
    print("peach is not in the list.")

# Use del to remove an element by index.
nums = [10, 20, 30, 40, 50]
del nums[2]
print(nums)  # [10, 20, 40, 50]

# Use del with a slice to remove a range of elements.
del nums[1:3]
print(nums)  # [10, 50]

# Use clear() to remove all elements.
nums.clear()
print(nums)   # []
print(len(nums))  # 0

# Use a list comprehension to filter out (remove) elements.
# Creates a new list containing only elements that meet the condition.
scores = [55, 80, 45, 92, 60]
passing = [s for s in scores if s >= 60]
print(passing)  # [80, 92, 60]

Overview

『pop()』returns the removed value, making it useful when you need to both remove and use the value at the same time. In contrast, 『remove()』simply deletes the element without returning anything. Since 『remove()』raises a ValueError if the value does not exist, use the 『in』 operator to check for the value beforehand, or handle the exception with try-except.

『del』is a Python keyword that can delete not only list elements but also variables themselves. Writing 『del my_list』removes the list variable entirely — this is different from 『clear()』, which only empties the list.

Be careful when removing elements from a list inside a loop. Using 『remove()』or 『pop()』inside a 『for』loop can cause index misalignment and unexpected behavior. For safe removal during iteration, loop over a copy of the list (『list[:]』) or use a 『list comprehension』to build a new list.

If you find any errors or copyright issues, please .