Python

Python List Pop

Several times when programming, a programmer needs to remove the very last item from a list. The pop() method eliminates and returns the final item of the list if there is no index. This function returns the element you deleted, permitting you to see what variations were made to your list. The index of the element you have to remove is the only parameter to pop(). You might opt to remove a particular item from a list. Assume you are creating a program to keep track of the books accessible in a bookstore. When a book is taken out, you may choose to remove it from the set of possible books and return it. Pop is a built-in method in the Python language that may be used to eliminate an item from a list. The pop() method returns the removed item after removing an element from a given place in a list.

This article will look at the pop() function in Python, how it’s utilized, and what parameters it accepts. Then, to demonstrate how the function can be used with lists, we will look at a few examples.

Example 1

The first illustration is the easiest one regarding the python pop function. We have a file titled “temp.py” that will be utilized. This file will be compiled in “Spyder IDE”. In this example, a list of “Fruits” has been initialized at the start. We have listed the names of five different fruits in it. After that, we have called the pop() function for the list and stored its value in a variable. Then we have taken the print by using print statements. The first print statement will print the return value of the list. However, the second one will display the updated list after the pop() function.

  • Fruits = [‘apple’, ‘banana’, ‘pome’, ‘strawberry’, ‘water melon’]
  • return_value = fruits.pop(4)
  • print(‘The Return Value of fruits are :’, return_value)
  • print(‘Updated List:’, Fruits)

Once you write the code in your file, save it and compile it by clicking on the “Run” option. If you see the black screen, this means the code is correct. The return value and the updated list are displayed on the console screen.

Example 2

The second example is explained regarding the python pop function. We have a file titled “temp.py” that will be utilized. This is a similar file that we have used in the previous example. This file will be compiled in “Spyder IDE”. In this example, a list of “Values” has been initialized. Then we have the print statement that will display “When we have not passed any sort of index:” This means that no index value has been initially passed. The pop () function will be called, and the result will be written on the screen. Then we passed the “-1” index and printed its values. After that, we passed the “-3” index and printed its value. Then, the updated list will be demonstrated on the console by utilizing the print function. You can also alter this program depending on your work.

  • Values = [‘one’, ‘two’, ‘three’, ‘four’, ‘five’]
  • print(‘When we have not passed any sort of index:’)
  • print(‘The return value will be: ‘, values.pop())
  • print(‘Updated List:’, Values)
  • print(‘\nWhen we have passed -1: ‘)
  • print(‘The Return Value will be:’, values.pop(-1))
  • print(‘Updated List:’, values)
  • print(‘\n When we have passed -3:’)
  • print(‘The Return Value will be:’, Values.pop(-3))
  • print(‘The Updated List:’, Values)

Once you write the code in your file, save it and compile it by clicking on the “Run” option. If you see the black screen, this means the code is correct. The return value and the updated list can be seen on the console screen after every iteration.

Conclusion

If you already have an existing array with a value that you would like to remove, the Python pop() method comes in handy. We learned how to use Python’s pop() method to delete data from a list in this article. Using examples, we have gone over the two methods for removing elements from a list. As a result, you can utilize any method that is suitable and useful for you. I hope this guide assisted you in understanding how to utilize the pop() method in Python List.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content