5 Techniques to Use List pop() Method in Python

Introduction

In Python, we have lists that store items of different data types in an ordered sequence. There are many ways to delete an element from the list, such as pop, remove, etc. In this tutorial, we will discuss the Python list pop() function through which we can remove the required element with the required index value.

What is List pop() in Python?

List pop() is the built-in function in Python to pop out the last element from the list if no parameter is passed. If the index value is passed, it will pop the element from the particular index of the list.

The average and the worst-case time complexity for Python list pop() are:

  • for the first and last element is O(1)
  • for the intermediate element is O(n)

Syntax of List pop() Method in Python

list.pop(index)

Parameter

Pop() method contains only a single parameter in the list i.e., Index.

  • If the index value is passed, it will pop the element of the particular index in the list.
  • If the index value is not passed, it will pop the last element of the list.
  • If the index value is out of range,, it throws IndexError: pop index out of range exception.

Return value

The pop() function returns the element present at the given index and removes the particular element from the list.

Techniques to Pop Elements From List in Python

Let us understand pop() function in detail with the help of different examples:

1. Removing / Pop element without passing index

In this example, we will take the input as a list. Then, we will not passing the index value in the pop() function. So, it should remove the last element from the list and return it.

#input list
lst = [1, 2, 3, 4, 5, 6, 7, 8, 9]

#pop() function
output = lst.pop()

#print pop() value and list
print("Popped value : ",output)
print("Remaining list : ",lst)

Output:

Popped value :  9
Remaining list :  [1, 2, 3, 4, 5, 6, 7, 8]

Explanation:

First, we have taken the input as a list in lst. Secondly, we have applied the pop() function in the list without passing the function’s index value. If we have not passed any index value in the list by default, it takes it as the last element of the list. Thirdly, we have printed the popped element from the list. At last, we have printed the list with the remaining elements. Hence, the output gets printed, and you can see the pop() function in the list.

2. Removing element while index passed

In this example, We will take the input as a list. Then, we will pass the index value in the pop() function. So, it should remove the passed index element from the list and return that element.

#input list
lst = [1, 2, 3, 4, 5, 6, 7, 8, 9]

#pop() function
output = lst.pop(2)

#print pop() value and list
print("Popped value : ",output)
print("Remaining list : ",lst)

Output:

Popped value :  3
Remaining list :  [1, 2, 4, 5, 6, 7, 8, 9]

Explanation:

First, we have taken the input as a list in lst. Secondly, we have applied the pop() function in the list, bypassing the index value of 2 in the function. If we have not passed any index value in the list by default, it takes it as the last element. Thirdly, we have printed the popped element from the list. At last, we have printed the list with the remaining elements. Hence, the output gets printed, and you can see the pop() function in the list.

3. List inside list

In this example, we will take input as a list and inside that another list and then remove the element from the list with the help of its index element.

#input list
lst = [1, 2, 3, [4, 5, 6, 7], 8, 9]

#pop() function
output = lst.pop(3)

#print pop() value and list
print("Popped value : ",output)
print("Remaining list : ",lst)

Output:

Popped value :  [4, 5, 6, 7]
Remaining list :  [1, 2, 3, 8, 9]

Explanation:

We have taken the input as a list in last, and inside the list, we have passed another list. Secondly, we have applied the pop() function in the list bypassing the index value as 3 in the function. If we have not passed any index value in the list by default, it takes it as the last element. Thirdly, we have printed the popped element from the list. At last, we have printed the list with the remaining elements. Hence, the output gets printed, and you can see the given function in the list.

4. Python List pop() using Passing index out of range

In this example, we will be taking input as a list. Then, we will calculate the list length with the help of the length function. In the pop() function, we will pass the index value greater than the length of the list and see the error as an index out of range.

#input list
lst = [1, 2, 3, 4, 5, 6, 7, 8, 9]

#length of list
c= len(lst)

#pop() function
output = lst.pop(c)

#print pop() value and list
print("Popped value : ",output)
print("Remaining list : ",lst)

Output:

Traceback (most recent call last):
  File "<string>", line 8, in <module>
IndexError: pop index out of range

Explanation:

First, we have taken the input as a list in lst. Secondly, we will calculate the list length with the help of Python’s length function. Thirdly, we have applied the pop() function in the list by passing the index value as the length of the list. The start index of the list is from 0. If we pass the exact length, it will also be treated as a value out of range. Fourthly, we have printed the popped element from the list. At last, we have printed the list with the remaining elements. Hence, the output gets printed, and you can see the pop() function in the list.

5. Passing the index value in negative

In this example, we will take the input as a list. Then, we will pass the pop() function, pass the index value in the negative, and see the output.

#input list
lst = [1, 2, 3, 4, 5, 6, 7, 8, 9]

#pop() function
output = lst.pop(-2)

#print pop() value and list
print("Popped value : ",output)
print("Remaining list : ",lst)

Output:

Popped value :  8
Remaining list :  [1, 2, 3, 4, 5, 6, 7, 9]

Explanation:

First, we have taken the input as a list in lst. Secondly, we have applied the list’s pop() function, bypassing the index value as -2. If we have not passed any index value in the list by default, it takes it as the last element. Thirdly, we have printed the popped element from the list. At last, we have printed the list with the remaining elements. Hence, the output gets printed, and you can see the pop() function in the list.

Must Read

Conclusion

In this tutorial, we learned about the pop() function in the list in Python. The pop() function helps us remove or delete the element from the particular index and the last also. We have discussed all the possible ways to pop the elements from the list in detail with the help of examples. So, you can use any way which you prefer is correct and easy for you.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments