Skip to content

Python List pop() Method Malli Spark By {Examples}

  • by

The list.pop() is used to remove and return an item from a given list at a specified index position. By default, it returns and removes the last item(at -1 index position) of the list. The pop() method is a powerful tool that can help you write more concise and efficient code when working with lists.

In this article, I will explain the list.pop() method syntax, parameter, and usage of how we can pop the item from the list at specified/default index with examples. For complete methods refer to List Methods in Python.

1. Quick Examples of List pop() Method

If you are in a hurry, below are some quick examples of the list pop() method.

# Quick examples of list pop() method

# Initialize list
technology = [‘Spark’,’Java’,’Python’,’Pandas’,’Pyspark’,’Hadoop’]

# Example 1: Use pop() method
# Remove the last element
result = technology.pop()

# Example 2: Remove the item at index 2 position
result = technology.pop(2)

# Example 3: Use negative indexing
# with pop() method
result = technology.pop(-3)

# Example 4:Pass index of out of range into pop()
lists = [2, 4, 6, 8]
result = technology.pop(-3)

# Example 5: Use try& except handle the IndexError
lists = [1, 2, 3, 4, 5]
try:
# Try to pop an element
# from an invalid index
removed_item = lists.pop(10)
except IndexError:
print(“pop index out of range”)

2. List pop() Method

Use pop() method from Python’s list to remove and return the item at a specified index position. It takes the index as an option argument, and when specified it returns the value of the given index. If an index is not provided, the pop() method will remove and return the last item from the list.

Note: If the specified index is out of range this function will raise an exception with IndexError.

2.1 Syntax of the List pop()

Following is the syntax of the list pop().

# Syntax of the pop()
list.pop(index)

2.2 Parameters of pop()

index – Is a optional parameter that specifies the index of the item to be removed from the list. If the index is not provided, then pop() will remove and return the last item in the list.

2.3 Return value

It returns the removed item from the list.

3. Pop the Item from Python List Example

Let’s initialize the list with some items and call pop()method over the list with no argument, it will remove and return the last item which is ‘Hadoop’. By default it ops the items from the end of the list.

# Initialize the list
technology = [‘Spark’,’Java’,’Python’,’Pandas’,’Pyspark’,’Hadoop’]

# Use pop() method
# To remove the last element
result = technology.pop()
print(“Popped item:”, result)
print(“List after pop():”,technology)

Yields below output.

As you can see, the list has been updated with out popped element.

4. Pop the Item at Specified Index from Python List

You can also pop an element from the list at any specific index by inputting index as argument, By calling pop() with index, it removes the element from the specified indeax and return the removed element. Let’ pass ‘2’ index position and remove its corresponding item from the given list.

# Remove the item at index 2 position
result = technology.pop(2)
print(“Popped item:”, result)
print(“List after pop():”,technology)

Yields below output.

5. Pass Negative Index into pop() Method

You can also use negative indexing with the pop() method to remove and return the item from the ending of the list. By default, it can remove the first item from the ending of the list. In this example, I will pass the -3 index into the pop() method, and remove the third element from the end of the list, which is ‘Pandas’.

# Use negative indexing
# with pop() method
result = technology.pop(-3)
print(“Popped item:”, result)
print(“List after pop():”,technology)

# Output:
# Popped item: Pandas
# List after pop(): [‘Spark’, ‘Java’, ‘Python’, ‘Pyspark’, ‘Hadoop’]

6. Use Try & Except Statement Handle the IndexError

The IndexError can be raised when we pass invalid index(out of range) into the pop() method. For example, the pop() method is called with an index of 10, which is not a valid index in our list, as it contains only 5 elements with indices ranging from 0 to 4. This will raise an IndexError with the message “pop index out of range”. For example,

# Pass index of out of range into pop()
lists = [2, 4, 6, 8]
result = technology.pop(10)
print(“Popped item:”, result)

Yields below output.

# Output:
Traceback (most recent call last):
File “./prog.py”, line 3, in <module>
IndexError: pop index out of range

Using try and except statemen,t we can handle the errors within our code in Python. Let’s apply try and except to the above code to handle the IndexError.

# Use try& except handle the IndexError
lists = [1, 2, 3, 4, 5]

try:
# Try to pop an element
# from an invalid index
removed_item = lists.pop(10)
except IndexError:
print(“pop index out of range”)

# Output:
# pop index out of range

7. Conclusion

In this article, I have explained the python list pop() method and using its syntax, parameters, and usage how we can pop the item at a specified index position or without an index. And also explained using try & except statement how we can handle the exception of IndexError.

Happy Learning !!

 The list.pop() is used to remove and return an item from a given list at a specified index position. By default, it returns and removes the last item(at -1 index position) of the list. The pop() method is a powerful tool that can help you write more concise and efficient code when working with lists.  Read More Python, Python Tutorial, Python List Methods 

Leave a Reply

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