Python List pop method with Example – Python Tutorial

3 weeks ago codedestine 0

In this tutorial, we will learn about python list pop method.
Python List Pop

pop method :

This method is used to remove and return the last element of a given list. If optional argument index is passed, then element at this index is remove and return, instead of last element. If value of optional argument index is greater than the length of the given list, then IndexError exception is raised.

Syntax : < List Object >.pop( index ) : 
              index : it is an optional argument, if provided then element at this index is remove and return. if not then element at last index is remove and return.

Example:

# Python List pop example # create list object and assign it to variable l l = ['a','b','c','d'] # call pop() method output = l.pop() # print the value of variable output print(output) # print the list, to check its elements print(l) # call pop() method by passing 1 as an argument output = l.pop(1) # print the value of variable output print(output) # print the list, to check its elements print(l)

References :-

  1. pop method Docs

That’s all for Python List pop method. If you liked it, please share your thoughts in comments section and share it with others too.

Previous