Python List pop()

The list pop() method removes the item at the specified index. The method also returns the removed item.

Example


Syntax of List pop()

The syntax of the pop() method is:

list.pop(index)

pop() parameters

  • The pop() method takes a single argument (index).
  • The argument passed to the method is optional. If not passed, the default index -1 is passed as an argument (index of the last item).
  • If the index passed to the method is not in range, it throws IndexError: pop index out of range exception.

Return Value from pop()

The pop() method returns the item present at the given index. This item is also removed from the list.


Example 1: Pop item at the given index from the list

Output

Return Value: French
Updated List: ['Python', 'Java', 'C++', 'C']

Note: Index in Python starts from 0, not 1.

If you need to pop the 4th element, you need to pass 3 to the pop() method.


Example 2: pop() without an index, and for negative indices

Output

When index is not passed:
Return Value: C
Updated List: ['Python', 'Java', 'C++', 'Ruby']

When -1 is passed:
Return Value: Ruby
Updated List: ['Python', 'Java', 'C++']

When -3 is passed:
Return Value: Python
Updated List: ['Java', 'C++']

If you need to remove the given item from the list, you can use the remove() method.

And, you can use the del statement to remove an item or slices from the list.