Python list pop function

The Python list pop() function removes an item from the existing list at a user-specified index and returns the deleted item as the output. By default, it removes the last item in a list. However, we can provide the index position to delete a specific item.

The Python list pop() function accepts both positive and negative index positions. If you pass a positive value, the search process starts from the list beginning (0 index position) and removes the item at the given index position. For a negative index value, the pop() function starts from the last item and moves towards the start position.

Python list pop() syntax

The syntax of the list pop() function to remove a list item at a given index position is

listName.pop()
listName.pop(index)

Here, the listName is the original list from which we want to remove an item. If you check the syntax of the Python list pop() function, the index argument is optional. It means we can use the index() method with or without an argument.

  • listName.pop(): As there is no mention of the index position, it removes the last item from a list. It means the default value is -1.
  • listName.pop(index): It removes the list item at the specified index position.

TIP: Remember, the Index position starts at 0 and ends at n-1. It means the first list value is at the 0th position and the last value is at the n-1 position.

Return Value: The Python list pop() function returns the removed item from the specified index position. We can use the return value to perform undo operations. If the given index position is out of range, pop() raises the IndexError.

Python list pop function with index

The pop() function removes one list item at the specified index and displays that element. After elimination, the remaining values move up to fill the index gap. If we know the Index value and want to see the deleted value, we can use the list pop() function to remove the item.

In the following example, we declared an integer list of four elements and used the list pop() function with an index argument.

As we mentioned earlier, the index value starts from 0, so the first item is at the 0th position and the last item is at the n-1 position. In the following code, we use the Python list pop() function multiple times to remove items at index positions 1 and 2. Here,

  • a.pop(1) removes the list item at the first index position, and that is 20 (2nd list item).
  • a.pop(2) removes the list item at the second index position, and that is the 3rd list item (40).

After a.pop(1) statement removes 20 from the list, the remaining items adjust the default to neutralize the index gap. So, 30 becomes 2nd item, and 40 becomes the third list item.

a = [10, 20, 30, 40]

a.pop(1)
print(a)

a.pop(2)
print(a)
[10, 30, 40]
[10, 30]

TIP: As the pop() function removes one item at a time, we must use the pop() method multiple times to delete multiple items.

Storing the return Value

In the previous example, we used the pop() function to delete list items. As the Python list pop() function returns the deleted item as output, we can declare a variable to assign the value.

In the following example, we used the pop() function to remove the 4th list item. We can display the eliminated item by assigning the value to a new variable. Here, we assigned the pop item to the variable a, and it holds that value. In the future, we can use it to undo the delete operation or use that value in any other logic.

n = [3, 5, 7, 9, 11, 15]

a = n.pop(3)
print(a)
print(n)
9
[3, 5, 7, 11, 15]

To undo the above process, we can use the insert() function with the index value to add the deleted value.

n = [3, 5, 7, 9, 11, 15]

a = n.pop(3)
print(a)
print(n)

n.insert(3, a)
print(n)

As you can see from the output, we have restored the deleted value from the Python list pop() function return value using the insert() function. To add the delete item to the last position, use the append() function.

9
[3, 5, 7, 11, 15]
[3, 5, 7, 9, 11, 15]

Removing Last Item

One of the most common questions asked on the internet is how to remove the last item from a list using the pop() function. We can use either the pop() function without an argument or use -1 as the index value. Both will remove and return the last item of the list.

n.pop()
n.pop(-1)

Python pop first, second list items

In this example, we declared a string list. Next, we used the method to remove items at index positions 1 and 2.

Fruits = ['Apple', 'Orange', 'Banana', 'Kiwi', 'Grape']
print("Original :", Fruits)
Fruit = Fruits.pop(1)
print("After Items are :", Fruits)
Fruit = Fruits.pop(2)
print("After Items are :", Fruits)
Python list Pop Function Example

TIP: Please take a look at the List and list functions articles to understand everything about them in Python.

It is another example of the Python list pop() method on a string to delete list items.

Fruits = ['Apple', 'Orange', 'Grape', 'Banana']

Fruit = Fruits.pop(1)
print(Fruits)
print('Item extracted = ', Fruit)
print('========')

Fruit = Fruits.pop(1)
print(Fruits)
print('Item extracted = ', Fruit)
print('========')

Fruit = Fruits.pop(1)
print(Fruits)
print('Item extracted = ', Fruit)
print('========')
['Apple', 'Grape', 'Banana']
Item extracted =  Orange
========
['Apple', 'Banana']
Item extracted =  Grape
========
['Apple']
Item extracted =  Banana
========

How to pop mixed list items?

We can use the Python pop() function on Mixed list items, and it works the same as the regular lists. Here, we declared a mix of numbers and strings(words). Next, we used the pop function to remove items at index 2.

MixFr = ['apple',  1, 'Orange', 5, 'Kiwi', 'Mango']

MixFr.pop(2)
print(MixFr)
['apple', 'Orange', 'Kiwi', 'Mango']

Python list pop() function without index

As we mentioned earlier, the index argument is optional, and we can use the pop() function without any argument.

In the following example, we declared an integer list of five items. Next, we utilized the pop() function without an index argument. If we don’t pass any argument, the pop() method uses the default index value of -1 and removes the last index item.

a = [1, 2, 3, 4, 5]
n = a.pop()
print(n)
print(a)
5
[1, 2, 3, 4]

NOTE: If you call the pop() function one more time, it removes 4 from the list, as it is the current last item.

Python list pop() function with negative index

In our previous examples, we have used the pop() function with positive index values and even without an index value. However, the pop() function allows the use of negative index numbers to remove list items.

When we use negative numbers as the index argument, the search process starts from the last item to the first item. It means that the last list item is at the -1 index position, its previous (last but one) is at the -2 position, and so on.

To demonstrate the Python list pop() function with negative index values, we use the integer list with seven items. From the below,

  • n.pop(-1) removes and returns the last list item.
  • n.pop(-3) removes and returns the 3rd item from the right side.
n = [10, 20, 30, 40, 50, 60, 70]
a = n.pop(-1)
print(a)
print(n)

b = n.pop(-3)
print(b)
print(n)
70
[10, 20, 30, 40, 50, 60]
40
[10, 20, 30, 50, 60]

Python list pop function on Nested List Items

If you use the pop() function on nested lists, it treats each sub-list as one item at that particular index. For instance, in the following example, we declared a nested list where the main list contains 5 sub-lists and each sub-list consists of two items.

Next, we call the pop() function to delete nested items at index positions 2 and 3. It means the pop() function must remove the sub-lists at the second ([11, 22]) and third ([99, 77]) index positions.

Nested = [[71, 222], [22, 13], [11, 22], [44, 55], [99, 77]]

Nested.pop(2)
print(Nested)

Nested.pop(3)
print(Nested)
[[71, 222], [22, 13], [44, 55], [99, 77]]
[[71, 222], [22, 13], [44, 55]]

Handle Index out of range IndexError in Python pop

As we all know, the pop() function uses the list index position to remove list items; we must be careful with passing values. If we pass an integer value greater than the last index position of a list, the pop() function raises an IndexError.

For example, if a list consists of 4 items and we pass 4 as the pop() function argument, it will raise an IndexError. As the index position starts from 0, the last available index position is 3, and no item exists in the 4th index position.

  • 0 -> 10
  • 1 -> 20
  • 2 -> 30
  • 3 -> 40
n = [10, 20, 30, 40]

a = n.pop(4)
print(a)
print(n)
IndexError: pop index out of range

To avoid these Python pop() function errors, we must ensure that the list is not empty and the specified index number is within the boundaries.

n = [10, 20, 30, 40]
val = 4
tot = len(n)
if tot > val:
    a = n.pop(val)
    print(a)
    print(n)
else:
    print("Index should be less than", tot)
Index should be less than 4

TIP: If you change the value to 2, it removes 30 from the list.

Empty List

For an empty list, we can follow a different approach.

n = []
val = 2
if n:
    a = n.pop(val)
    print(a)
    print(n)
else:
    print("List is empty")
List is empty

Otherwise, utilize the try exception block

n = []
try:
    item = n.pop()
except IndexError:
    item = None
print(item)
None

Using the Python list pop() function in iteration

As we mentioned earlier, the pop() function removes one item at a time. However, we can use the for loop or the while loop to iterate over the list items and apply the pop() function to individual items based on the index.

The following example uses the while loop to iterate over the list items. On each iteration, the pop() function removes the last item in a list and assigns it to a variable. It is very helpful with stack behaviour, LIFO (Last In First Out).

n = [10, 20, 30, 40]

while n:
    a = n.pop()
    print(a)
   
print(n)
40
30
20
10
[]

TIP: If you change a single line inside the while loop, we can convert the LIFO to FIFO (first in first out). Please replace a = n.pop() with a = n.pop(0), and the result will be 10 20 30 40 [].

If you are a fan of the for loop and want to use a for loop range instead of a while loop, follow the example below.

n = [10, 20, 30, 40]

for i in range(len(n)):
    print(n.pop(), end = " ")

print("\n",n)
40 30 20 10
[]

Difference between Python pop() function and remove()

Both the pop() function and the remove() method delete one item from the existing list. However, the pop() function uses the index position to remove a list item. On the other hand, the remove() function accepts a value (list item) to remove and deletes the first occurrence of it.

myList.pop(index)
myList.remove(itemName)

If you know the list item to delete, use the remove() method. To delete an item at a specific index position, use the pop() function.

The Python list pop() function returns the removed value as output, whereas the remove() function deletes the first occurrence and does not return any output.

  • n.pop(3) deleted 4th list item (3rd index position) and assigned the return value to a.
  • n.remove(7) deletes the actual list item 7 (original value in a list).
n = [1, 2, 3, 4, 5, 6, 7, 8]
a = n.pop(3)
print(a)
print(n)

n.remove(7)
print(n)
4
[1, 2, 3, 5, 6, 7, 8]
[1, 2, 3, 5, 6, 8]

Python list pop() function vs del keyword

Both the pop() function and the del keyword remove the list items based on their index position. However, there are a few differences between them.

pop()del
It removes the list item at the specified index.It deletes one or more list items at the specified index.
By default, it removes the last item.We must specify what it has to delete. No default value.
The pop() function deletes a single item.We can pass the start and end positions to delete multiple list items.
It returns the deleted item.The del keyword won’t return any output.
Its syntax myList.pop() myList.pop(index)Its syntax del myList[index] del myList[start:end]
We can only remove items.It can be used to replace the list items.
Error: Index out of range: IndexErrorError: Index out of range: IndexError

In the following example, the Python list pop() function deletes the list item at the 3rd index position. On the other hand, the del keyword removes the last list item.

n = [1, 2, 3, 4, 5, 6]

a = n.pop(2)
print(a)
print(n)

del n[-1]
print(n)
3
[1, 2, 4, 5, 6]
[1, 2, 4, 5]

TIP: Instead of -1, use start and end indices like del n[2:4] to remove 4 and 5 from a list.