Python Tutorial Python Advanced Python References Python Libraries

Python List - pop() Method



The Python pop() method is used to delete an element at specified index of the list. If index number is not specified as parameter then it deletes last element from the list.

Syntax

#deletes last element of the list.
list.pop()  

#deletes element at specified index from the list.    
list.pop(idx)      

Parameters

idx Optional. index number of the element which need to be deleted from the list. default value is -1 which indicates last element of the list.

Return Value

Returns the deleted element of the list.

Example: Delete last element from the list

In the example below, list pop() method is used without any argument to delete last element from the list called MyList.

MyList = ['JAN', 'FEB', 'MAR', 'APR', 'APR']
MyList.pop()   
print(MyList)

The output of the above code will be:

['JAN', 'FEB', 'MAR', 'APR']

Example: Delete the specified element from the list

In this example, list pop() method is used to delete an element at index = 1 from the list called MyList.

month = ['JAN', 'FEB', 'FEB', 'MAR', 'APR']
month.pop(1)   
print(month)

The output of the above code will be:

['JAN', 'FEB', 'MAR', 'APR']

❮ Python List Methods

5