Python List pop() Method with Examples

In the previous article, we have discussed Python List insert() Method with Examples
List in Python:

Lists in Python are mutable sequences. They are extremely similar to tuples, except they do not have immutability constraints. Lists are often used to store collections of homogeneous things, but there is nothing stopping you from storing collections of heterogeneous items as well.

List pop() Method in Python:

The pop() method removes the item at the specified index from the list and returns the item that was removed.

Note: By default, it removes the last element from the list.

Syntax:

list.pop(index)

Parameters

The pop() method only accepts one argument (index).

index: This is optional.

  • It is not required to pass an argument to the method. If no argument is provided, the default index -1 is used (index of the last item).
  • If the index passed to the method is not within the range, the IndexError: pop index out of range exception is thrown.

Return Value:

The pop() method returns the item at the specified index. This item has been removed from the list as well.

Examples:

Example1:

Input:

Given list = ['good', 'morning', 'btechgeeks', 4, 5, 6]
Given index = 2

Output:

The value returned is:  btechgeeks
The given list after poping 2 item =  ['good', 'morning', 4, 5, 6]

Example2:

Input:

Given list = ['hello', 'this', 'is', 'btechgeeks']

Output:

The value returned is:  btechgeeks
The given list after poping with giving an index is : ['hello', 'this', 'is']

List pop() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the list as static input and store it in a variable.
  • Give the number(index) as static input and store it in another variable.
  • Pass the given index as a parameter to the pop() function for the given list (which removes the element at the given index and returns the value that is removed).
  • Store it in another variable.
  • Print the above-returned value.
  • Print the given list after poping the item at the given index value.
  • The Exit of Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvn_lst = ['good', 'morning', 'btechgeeks', 4, 5, 6]
# Give the number(index) as static input and store it in another variable.
gvn_indx = 2
# Pass the given index as a parameter to the pop() function for the given
# list (which removes the element at the given index and returns the value
# that is removed).
# Store it in another variable.
retrnvalu = gvn_lst.pop(gvn_indx)
# Print the above-returned value.
print("The value returned is: ", retrnvalu)
# Print the given list after poping the item at the given index value.
print("The given list after poping", gvn_indx, "item = ", gvn_lst)

Output:

The value returned is:  btechgeeks
The given list after poping 2 item =  ['good', 'morning', 4, 5, 6]

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Give the number(index) as user input using the int(input()) function and store it in another variable.
  • Pass the given index as a parameter to the pop() function for the given list (which removes the element at the given index and returns the value that is removed).
  • Store it in another variable.
  • Print the above-returned value.
  • Print the given list after poping the item at the given index value.
  • The Exit of Program.

Below is the implementation:

# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(map(int, input(
    'Enter some random List Elements separated by spaces = ').split()))
# Give the number(index) as user input using the int(input()) function 
# and store it in another variable.
gvn_indx = int(input("Enter some random number = "))
# Pass the given index as a parameter to the pop() function for the given
# list (which removes the element at the given index and returns the value
# that is removed).
# Store it in another variable.
retrnvalu = gvn_lst.pop(gvn_indx)
# Print the above-returned value.
print("The value returned is: ", retrnvalu)
# Print the given list after poping the item at the given index value.
print("The given list after poping", gvn_indx, "item = ", gvn_lst)

Output:

Enter some random List Elements separated by spaces = 25 60 56 13 12
Enter some random number = 3
The value returned is: 13
The given list after poping 3 item = [25, 60, 56, 12]

Similarly, pop without an index and negative index

Approach:

  • Give the list as static input and store it in a variable.
  • Give the negative number(index) as static input and store it in another variable.
  • Pass the given index as a parameter to the pop() function for the given list (which removes the element at the given index and returns the value that is removed).
  • Store it in another variable.
  • Print the above-returned value.
  • Print the given list after poping the item at the given index value.
  • Apply the pop() function to the given list without giving an index and get the value that is returned.
  • Store it in another variable.
  • Print the above-returned value.
  • Print the given list after applying the pop() function.
  • The Exit of Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvn_lst = ['hello', 'this', 'is', 'btechgeeks']
# Give the negative number(index) as static input and store it in
# another variable.
gvn_indx = -3
# Pass the given index as a parameter to the pop() function for the given
# list (which removes the element at the given index and returns the value
# that is removed).
# Store it in another variable.
retrnvalu = gvn_lst.pop(gvn_indx)
# Print the above-returned value.
print("The value returned is: ", retrnvalu)
# Print the given list after poping the item at the given index value.
print("The given list after poping", gvn_indx, "item = ", gvn_lst)
# Apply pop() function to the given list without giving an index and get the
# value that is returned.
# Store it in another variable.
retrnvalu1 = gvn_lst.pop()
# Print the above-returned value.
print("The value returned is: ", retrnvalu1)
# Print the given list after applying pop() function
print("The given list is after applying pop() function:", gvn_lst)

Output:

The value returned is:  this
The given list after poping -3 item =  ['hello', 'is', 'btechgeeks']
The value returned is:  btechgeeks
The given list is after applying pop() function: ['hello', 'is']

Know about the syntax and usage of functions to manipulate lists with the provided Python List Method Examples and use them.