Python List pop() Method

The pop() method in Python is a built-in function used to remove and return an item from a list. The method removes the item from the list and returns the item. This method can also be used to remove the last item from the list if no index is specified.

Syntax

list.pop(index)

Parameters

  • index: (optional) The index of the item to be removed and returned from the list. If not specified, the last item in the list is removed and returned.

Examples

Example 1: Removing the Last Item from a List

Let’s consider a list and try to remove the last item from the list using the pop() method:

a = ["dog", "cat", "horse", "bird"]
item = a.pop()
print(item)
print(a)

In this example, we have created a list a containing four elements. We have used the pop() method without an index to remove the last item from the list. The method removes the last item from the list and returns the item. We have assigned the returned item to a variable item and printed it, which will be bird. Finally, we have printed the list to see the result, which will be ['dog', 'cat', 'horse'].

Example 2: Removing an Item from a List by Index

Let’s consider a list and try to remove an item from the list using the pop() method with an index:

a = ["dog", "cat", "horse", "bird"]
item = a.pop(1)
print(item)
print(a)

In this example, we have created a list a containing four elements. We have used the pop() method with an index 1 to remove the item at index 1 from the list. The method removes the item from the list and returns the item. We have assigned the returned item to a variable item and printed it, which will be cat. Finally, we have printed the list to see the result, which will be ['dog', 'horse', 'bird'].

Example 3: Removing an Item from a List that Does Not Exist

If the specified index is not found in the list, the pop() method raises an IndexError:

a = ["dog", "cat", "horse", "bird"]
try:
    item = a.pop(10)
    print(item)
    print(a)
except IndexError as e:
    print("Index not found in the list")

In this example, we have created a list a containing four elements. We have used the pop() method with an index 10 to remove the item at index 10 from the list. However, the index 10 is not found in the list. So, the pop() method raises an IndexError. We have used a try-except block to catch the error and print a message Index not found in the list. This is a useful technique when you want to avoid the program from crashing when an error occurs.

Conclusion

In this article, we have discussed the pop() method in Python and how it can be used to remove and return an item from a list. The method can be used with or without an index to remove the last item or a specific item from the list. The returned item can be assigned to a variable for further use. If the specified index is not found in the list, the pop() method raises an IndexError.

I hope you found this article helpful. Let me know in the comments section if you have any questions or suggestions.

Leave a Reply

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