Join Regular Classroom : Visit ClassroomTech

pop() method in Python List

It is a method in list that pops out an item from a specified position in the list. It returns the popped item and modifies the original list.

Syntax:

list.pop(position)
*Position is optional, by default it’s -1 and it pops out the last item.

Don’t know what is insert method in python? Read: insert() method in Python List

Example 1:

s = [2, 8, 7, 9, 33, 45, 52]
x = s.pop()
print(x)
print(s)

Output:

52
[2, 8, 7, 9, 33, 45]

Explanation:

Here the method pops out the last element “52” from the end of the list. The original list is modified as the element is popped out of the list.

Example 2:

s = [2, 8, 7, 9, 33, 45, 52]
x = s.pop(1)
print(x)

Output:

8

Explanation:

Here the method popped out the element at index 1 i.e. “8” from the original list

Example 3:

s = ["Name", "codewindow", "Country", "India"]
s.pop(2)
print(s)

Output:

[‘Name’, ‘codewindow’, ‘India’]

Explanation:

Here the element “Country” which was at index 2 was popped out from the original list.


You Missed

Also Checkout