Pop() List Python

In this Python tutorial, you will learn about pop() list Python to remove the elements from the list.

While building the to-do list application in Python, I needed to implement the functionality of removing a task when it was completed. So, I used the Python pop() method to remove the task dynamically.

In this tutorial, I have explained how to use the pop() method to remove an item from the list. I have also explained how to remove items from the list using the index and without the index.

Additionally, I have explained how negative indexing concepts can be used to remove items from the end of the list. After completing this tutorial, you will be able to remove any item from the list.

Let’s learn,

Pop() List Python

The Python pop() method allows you to remove items from the list based on their index. As you know, the list consists of a set of ordered items. Also, you know, you can access each item through its index.

The syntax of the pop() method is given below.

list_name.pop(index_value)

Where,

  • list_name: It is the name of the list whose items you want to remove using the pop() method.
  • pop(index_value): It is the pop() method that removes the item from the list based on the specified index_value of that item.

But remember, if you specify the index that doesn’t exist, then the pop() method raises an error called IndexError.

Let’s take an example, and understand how to use the pop() method to remove the elements from the list.

READ:  Python Check if a variable is a number

Suppose you have a list of states in a list called us_states which is shown below.

us_states = ["California", "New York", "Texas", "Florida"]

Now remove the items at the index 3 using the pop() method.

popped_state = us_states.pop(3)
print("The removed items:",popped_state)  
print("list after removing the states:",us_states)  
Pop() List Python

From the output, you can see that the items at index 3 are removed from the list using the pop() method.

Let’s understand the code part: popped_state = us_states.pop(3). This line of code uses the pop(3) method on the list ‘us_states’ and accepts the index value 3, which represents the list of items that you want to remove.

After executing that line of code, the items at index 3 in the list, which is ‘Florida’, are removed from the list.

Similarly, you can remove any items from the list using the pop() method by specifying the index value.

Pop() List Python without Index

In the above section, you have removed the items from the list using the pop() method with index, but you can also remove the items from the list without specifying the index value.

In this case, the pop() method removes the items from the last of the list. Let’s check with an example.

Suppose you have a list of us car brands which is shown below.

us_car_brands = ["Ford", "Chevrolet", "Tesla", "Dodge"]

To remove the last item from the above list, the “Dodge”, just call the pop() method on the list, as shown below.

us_car_brands.pop()
print(us_car_brands)
Pop() List Python without Index

From the above output, you can see that the last item is removed from the list using the pop() method without specifying the index value.

READ:  How to Remove Character from String Python By Index

In this line of code us_car_brands.pop(), the pop() method removes the last item, “Dodge”, from the list us_car_brands.

If you call the pop() method three times more, it will empty the list because it now contains only three items.

Python List pop() with Negative Indexing

Using the pop(), you can remove the items from the last of the list by specifying the negative index.

For example, you have a list of us_presidents which is shown below.

us_presidents = ["George Washington", "John Adams", "Thomas Jefferson", "James Madison"]

To remove the third last items or our present from the list, you can specify the index -3 within the pop() method as shown below.

us_presidents.pop(-3)

print(us_presidents)
Python List pop() with Negative Indexing

From the above output, you can see that the items at the index -3 are removed from the list using the pop() method.

Let’s understand the code part, us_presidents.pop(-3), which calls the pop() method with a negative index of -3 on the list ‘us_presidents’ to remove the third-last item, the “John Adams”.

Similarly, you can remove any items from the end of the list by specifying the negative indexing.

From the above examples, I hope that you understand how to use the pop() method to delete any items from the list.

Conclusion

In this Python tutorial, you learned about pop() list Python, where you learned about how to use the pop() method with syntax.

Also did multiple examples using the pop() method to remove the items from the list, such as you using the index to remove the items from the list based on a specific index. Also used the negative indexing to remove the items from the last of the list.

READ:  How to Prepend to a List in Python [6 Methods]

You may like to read: