Python List pop() Method Tutorial

Python List type provides the pop() method which is used to remove items with the specified index number. The pop() method is one of the fundamental methods related to the list type.

pop() Method Syntax

The pop() method has the following syntax. The pop() method returns the removed or popped item.

LIST.pop(INDEX)
  • LIST is a list which contains items where the specified INDEX item is removed.
  • INDEX is the index number we want to remove. The INDEX can be positive or negative index number.

Remove List Item with the Specified Index

One of the most used cases for the pop() method is providing a positive index number to select remove from the list and return as the pop() method result. The Python list index numbers start from 0 . In the following example, we pop the item with the index number 2.

names = ["ismail","ahmet","ali","elif"]

name = names.pop(2)

print(name)
ali

Remove List Item with Negative Index

The Python list can be used with negative index numbers. Negative index numbers are interpreted in reverse order where the -1 is the last item in a list. In the following example, we use negative index numbers to pop items.

names = ["ismail","ahmet","ali","elif"]

name = names.pop(-1)

print(name)
elif

Pop Last Item without Index

The pop() method can be used without an index number. By default, the pop() method without index pops the last item of the list.

names = ["ismail","ahmet","ali","elif"]

name = names.pop()

print(name)

Leave a Comment