Python list pop() function examples [Beginners]

Related Searches: python pop, python list pop, list pop, pop function python, python array pop, python pop function, pop method python, pop 0, python pop from list, array pop python, pop method, python string pop, pop from list python, pop python list, python pop 0, how to use pop in python, python pop method, pop array, list pop python 3, arraylist pop, pop in list, python list pop all, python pop all elements from list, python pull from list, pop 1 python, p pop python, what does pop mean in python, remove pop python, pop a value from list python, python list pop by value, how to use pop, pop number list, how to pop element from list in python, how to pop in, how does pop work, pop in array, pop list in python, pop python example, list pop 0 python

 

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.

Advertisement

 

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.

Advertisement
# 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:

Advertisement

 

[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:

Advertisement
[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

The time complexity of the python list pop() function is constant O(1). It does not matter how many elements are in the list, removing an element from a list takes the same time and it does not depend on the number of elements to be popped. The reason for this is that in cPython lists are implemented with arrays. And retrieving or removing an element from an array has constant time complexity. Thus the python list pop() function has constant runtime complexity as well.

So the time complexity of the python list pop() function is O(1).

 

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:

Python list pop() function with examples [Beginners]

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

pyhon list pop() function
pop() function in python
methods to remove elements in python

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment