Python List pop() Method Tutorial

In this section, we will learn what the List pop() method is and how to use it in Python.

Python List pop() Method

The Python list pop() method is used to remove an element from a list.

By default, calling this method will remove the last element of a list and return that as a result, but you can specifically decide at what index you want to remove an element.

List pop() Method Syntax:

list.pop(index)

List pop() Method Parameter:

The method takes one argument, and that is the index number that we want to remove the element there.

List pop() Method Return Value:

The return value of this method is the removed element of the list.

Example: python remove first element from list

li = ["ItemOne","ItemTwo","ItemThree","ItemOne","ItemTwo","ItemThree"]

print("The first element of the list is removed now and it is: "+li.pop(0))

Output:

The first element of the list is removed now and it is: ItemOne

Example: list pop in python

li = ["ItemOne","ItemTwo","ItemThree","ItemOne","ItemTwo","ItemThree"]

print("The size of the list before calling the pop() two times: ")

print(len(li))

print(li.pop(0))

print(li.pop(2))

print("The size of the list before calling the pop() two times: ")

print(len(li))

Output:

The size of the list before calling the pop() two times:

6

ItemOne

ItemOne

The size of the list before calling the pop() two times:

4
Facebook
Twitter
Pinterest
LinkedIn

Top Technologies