SyntaxStudy
Sign Up
Home Python Reference pop() / remove() / clear()

pop() / remove() / clear()

method

pop() removes and returns item at index; remove() removes first occurrence of value; clear() empties the list.

Syntax

list.pop(index=-1) list.remove(value) list.clear()

Example

python
lst = ['a', 'b', 'c', 'd']
lst.pop()      # removes 'd', returns 'd'
lst.pop(0)     # removes 'a', returns 'a'
# lst is now ['b', 'c']

lst2 = [1, 2, 3, 2, 1]
lst2.remove(2)  # removes first 2 -> [1,3,2,1]

lst2.clear()    # []