Python List pop()

List in Python provides a method pop() to delete elements from list by index positions. It also returns the deleted element.

Syntax of List pop()

The syntax of List pop() method is as follows,

list.pop(index)

Parameters of pop() method

  • index: It accepts an index position as an argument, and deletes the element at the index position. It is an optional parameter, if it is not provided then default value of -1 will be used. So, in case of default value, the last element of list will get deleted.

Return Value from pop() method

  • It returns the deleted element i.e. the item at the provided index position. It also deletes that element from list.

Important Point:

If the given index value does not exists in the List, then the pop() method will raise an error i.e. IndexError: pop index out of range exception.

Examples of List pop() method

Example 1: Delete element from List by Index

Suppose we have a List of names, and we want to delete the 4th name from the list. As indexing in Python starts from 0, so the index position of 4th item will be 3. So, to delete 4th item from list, we will call the pop() method with index value 3. It will return the deleted value.

Let’s see the complete example,

names = ['Ritika', 'Sanjay', 'John', 'Mathew', 'Rick', 'Smriti']

# Original List
print('Original List:', names)

# Delete item at index 3 i.e. 4th element
deletedName = names.pop(3)

print('Deleted Name:', deletedName)

# Updated List
print('Updated List:', names)

Output:

Original List: ['Ritika', 'Sanjay', 'John', 'Mathew', 'Rick', 'Smriti']
Deleted Name: Mathew
Updated List: ['Ritika', 'Sanjay', 'John', 'Rick', 'Smriti']

We called the pop() method with parameter 3. It deleted the element at index 3 from the list i.e. ‘Mathew’. It also returned the deleted value.

Example 2: List pop() and IndexError exception

If we pass an invalid index position as parameter in the pop() method, then it will raise IndexError. By invalid index, we mean an index position that does not exist in List. For example, if out list has size 6, and we try to delete element at index 10 using the pop() method, then it will raise an IndexError. For example,

names = ['Ritika', 'Sanjay', 'John', 'Mathew', 'Rick', 'Smriti']

# Delete item at index 10 i.e. 11th element
deletedName = names.pop(10)

print('Deleted Name:', deletedName)

# Updated List
print('Updated List:', names)

Output:

Traceback (most recent call last):
  File "temp.py", line 8, in <module>
    deletedName = names.pop(10)
IndexError: pop index out of range

As index 10 does not exist in the List, so it raised an error.

Example 3: using List pop() with try/except

To handle the IndexError related issue, we can use the try/except blocks. Let’s see the complete example,

names = ['Ritika', 'Sanjay', 'John', 'Mathew', 'Rick', 'Smriti']

try:
    # Delete item at index 10 i.e. 11th element
    deletedName = names.pop(10)

    print('Deleted Name:', deletedName)

    # Updated List
    print('Updated List:', names)
except IndexError:
    print('Given Index is invalid.')

Output:

Given Index is invalid.

Summary

We learned about the details of List’s pop() method in Python. Also, understood its usage details.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top