Introduction to Python list pop() function
Suppose that we might want to remove some specific elements from a list. Python provides us multiple ways to achieve this goal. One of the main built-in methods in python which is used to remove elements from a list is python list pop()
function. In this tutorial, we will learn about the python pop()
function, and we will cover some of the scenarios through examples and will use the pop()
function in multiple ways to remove specific elements. Moreover, we will also cover some of the alternative methods that we can use instead of python pop()
functions. By the end of this tutorial, you will have a full understanding of the python list pop()
function.
Getting start with python list pop() function
The python list pop()
function removes an element present at a specified index from the list. It returns the popped element. A simple syntax of python list pop is given below:
list_name.pop(Index_number)
The python list pop()
function takes an optional single argument. If the argument or the index number is not provided, the default will be the index of the last element. In this section, we will pop up elements from the list through various ways.
Python List pop() function by index
As we mentioned we can remove elements using the pop()
function by passing an index number. The python list pop()
function will return the removed element. See the following example.
# list of number
myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# python list pop without any index
myList.pop()
# printing
print(myList)
Output:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Notice that we didn't specify the index number and the pop()
function by default removed the very last element of the list.
Now let us take one more example, this time we will pass an index number as an argument.
# list of number
myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# python list pop
myList.pop(3)
# printing
print(myList)
Output:
[1, 2, 3, 5, 6, 7, 8, 9, 10]
Now if we try to print the pop()
function itself, we will get the removed/popped element itself. See the example below:
# list of number
myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# python list pop
print(myList.pop(3))
Output:
4
Python List pop() function from the front of list
You might know that indexing in python starts from 0 rather than 1. So if we want to remove the very first element of the list we should use index as 0 ( not 1). See the example below which removes the very first element of our list.
# list of number
myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# python list pop() function
myList.pop(0)
# printing
print(myList)
Output:
[2, 3, 4, 5, 6, 7, 8, 9, 10]
Python list pop by values
Python allows us to remove elements not only by index but also through passing values as well. Actually, it works in a similar way, but this time we will not explicitly mention the index number, rather we will give a value and python will find the index number and will remove it using the pop()
function. Here is the simple syntax of pop lists by values.
mylist.pop(mylist.index(value))
Now let us take an example and remove the number 7 from the list using the above method.
# list of number
myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# python list pop using value
myList.pop(myList.index(7))
# printing list
print(myList)
Output:
[1, 2, 3, 4, 5, 6, 8, 9, 10]
Notice that mylist.index()
method actually returns the index number of a given value. See the example below which confirms it.
# list of number
myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# printing the index value
print((myList.index(7)))
Output:
6
Python list pop first n elements
Suppose we want to pop or remove multiple elements from a list, python pop()
function does not allow us to remove multiple elements directly. But indirectly we can remove multiple elements using simple list comprehension statements.
Let us say we want to remove the first 5 elements from our list. To do that we have to create a list of values using list comprehension and for loop to pop for elements. See the example below to understand this phenomenon.
# list of number
myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# popping multiple elements
popped_element = [myList.pop(0) for i in range(5)]
# printing original list
print(myList)
Output:
[6, 7, 8, 9, 10]
The popped_element list contains the first 5 elements because pop()
function returns the removed element.
Python list pop last n elements
In a similar way, we can use list comprehension to remove the last n elements. The only difference is that this time we don't have to pass the index number because by default the pop()
function will remove the very last element each time. Let us say we want to remove the last five elements, see the example below of how we achieve this.
# list of number
myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# popping multiple elements
popped_element = [myList.pop() for i in range(5)]
# printing original list
print(myList)
Output:
[1, 2, 3, 4, 5]
More about Python List pop() function with examples
So far we learned about the basic syntax of the python list pop()
function and we covered various ways to pop up elements from the list. In this section, we will take one more step ahead and cover some advanced concepts related to the pop()
function, for example, time complexity and iterating.
The time complexity of Python List pop() function
Time complexity is a measure of how quickly your code can perform a task in relation to its inputs. As the number of inputs increases, time complexity tells you at what rate your code will slow down. This is where algorithmic complexity comes in to play, otherwise known as Big O Notation . It is the concept to describe how long an algorithm or program takes to execute. Take a list, for example. As the number of items within the list grows, so does the amount of time it takes to iterate over the list. This is known as O(n)
, where n represents the number of operations. It’s called Big O Notation because you put a “Big O
” in front of the number of operations.
Python lists are ordered collections of elements and, in Python, are implemented as resizable arrays. An array is a basic data structure that consists of a series of contiguous memory locations, and each location contains a reference to a Python object.
Accessing or modifying an element involves fetching the object reference from the appropriate position of the underlying array and has complexity O(1)
. Appending an element is also very fast. When an empty list is created, an array of fixed size is allocated and, as we insert elements, the slots in the array are gradually filled up. Once all the slots are occupied, the list needs to increase the size of its underlying array, thus triggering a memory reallocation that can take O(N)
time. Nevertheless, those memory allocations are infrequent, and the time complexity for the append operation is referred to as amortized O(1)
time.
In the following table, the timings for different operations on a list of size 10,000 are shown; you can see how insertion and removal performances vary quite dramatically if performed at the beginning or at the end of the list:
Code | N=10000 (µs) | N=20000 (µs) | N=30000 (µs) | Time |
---|---|---|---|---|
list.pop() | 0.50 | 0.59 | 0.58 | O(1) |
list.pop(0) | 4.20 | 8.36 | 12.09 | O(N) |
So pop()
for the last element ought to be O(1)
since you only need to return the element referred to by the last element in the array and update the index of the last element. While pop()
for an arbitrary element to be O(N)
as to perform a pop operation, it has to go through all the elements in the list to find the respective element.
Python List pop and iteration
We already had touch popping of elements using iteration while discussing removing elements using list comprehension. In this section, we will explicitly use python interaction to pop elements from a list. See the example below which removes all the elements of list through iteration.
# list of number
myList = [1, 2, 3, 4, 5]
# for loop
for i in range(len(myList)):
# removing elements
myList.pop()
# printing list
print(myList)
Output:
[]
Notice that the list is empty because we have removed all the elements using python iteration method.
Python List pop out of range
Sometimes we might pop up an element that is out of the index. In such cases, we will get an error of out of index. In this section, we will cover how we can handle such an error using the if-else statement. See the error in the following example.
# list of number
myList = [1, 2, 3, 4, 5]
# for loop
for i in range(6):
print(myList.pop())
Output:
We get an error because pop()
functions still try to remove elements from an empty list. Now see the following example how we can handle such an error
# list of number
myList = [1, 2, 3, 4, 5]
# for loop
for i in range(6):
print(myList.pop() if i else None)
Output:
None
5
4
3
2
1
Alternative ways of Python list pop() function
The Python pop list method is not the only method to remove elements from a list. There are many other various ways as well and some of which we will discuss in this section.
See the list of different methods below which remove elements from a list.
- remove method: Remove an element from the list (by value)
- clear method: Remove all elements from the list
- delete method: Remove one or more elements from the list (by index or slice
Python remove method
The python list remove is a built-in function in the python programming language which removes a given object from a list. Simple syntax of remove method looks like this:
list_name.remove(element)
Now let us take an example of out previous list and let us remove elements by using remove method.
# list of number
myList = [1, 2, 3, 4, 5]
# remove method in python
myList.remove(3)
# printing list
print(myList)
Output:
[1, 2, 4, 5]
Notice that we have successfully removed element 3 from our list.
One of the very important uses of the remove method is that we can easily remove all the duplicates of an element. Now see in the following example how we removed all 1's from our list without writing too much code.
# list of number
myList = [1, 2,1, 3,1,1 ,4, 5,1,1,1,1,1]
# while loop
while (myList.count(1)):
# remove element one element
myList.remove(1)
# printing list
print(myList)
Output:
[2, 3, 4, 5]
Python clear method
The Python clear method is a built-in function that removes all the elements from a list. It clears the list completely and returns nothing. It does not require any parameter and returns no exception if the list is already empty. Here is a simple syntax of the python clear method.
list_name.clear()
Now let us take a simple example and use a clear method to remove all the elements.
# list of number
myList = [1, 2, 3, 4, 5]
# clear method
myList.clear()
# printing
print(myList)>/code>
Output:
[]
Now let us try to print a clear method to make sure its return value.
# list of number
myList = [1, 2, 3, 4, 5]
# clear method
print(myList.clear())
Output:
None
Notice that, unlike the python pop()
function, the clear method returns nothing.
Python delete
Delete method is also famous among python beginners. It deletes the specified index value. One of the importance of delete is that it allows us to slice out list and delete the sliced one. The simple syntax of the python delete element looks like this.
del list_name[index_number]
Now let us take an example and delete element 5 from our list.
# list of number
myList = [1, 2, 3, 4, 5]
# delete
del myList[4]
# printing
print(myList)
Output:
[1, 2, 3, 4]
Now here is a simple syntax of removing a sliced list.
del list_name[starting:ending]
Now let us take an example of deleting a sliced list.
# list of number
myList = [1, 2, 3, 4, 5]
# delete
del myList[1:4]
# printing
print(myList)
Output:
[1, 4, 5]
Summary
Python pop list is a built-in function that removes elements from a specified position and returns that value. If the index number is not specified, then by default it will remove the very last element of the list. In this tutorial, we covered everything that we need to learn about the python pop()
function. We learned about different ways to implement the pop()
function using various examples. Moreover, we also come across some of the alternative ways which we can also use to remove elements from the list.
Further Reading
pop() function in python
methods to remove elements in python
Hey the time complexity for pop() isn’t constant unless you’re removing from the end of the list. Because pop would need to shift all elements in the list that are after the delete item by 1. So pop(0) would have to do an operation on every item in the array, meaning pop has a O(n).
Honestly, it worries me greatly that a tutorial website has this wrong. Especially cuz the reasoning used to explain O(1) shows the author doesn’t understand how arrays work, which is a fundamental container in plenty of data structures.
Thank you for highlighting this. We have corrected the chapter and added more information for better understanding with some performance data.
The updated and added information is a very good read/explantation! Thank you