89DEVs

Python list.pop() method

list.pop() method

The pop() method returns the element at the given index in the list. And it removes the element from the list afterwards. The list.pop() method makes it possible, to return an element by index and delete it with just one function call. The same can be achieved by selecting the element and then using the list.remove() method to remove it. However, this operation is much simpler by using the list.pop() method.

first element of list using list.pop()

To pop the first element of a list, use the list.pop() method and the index 0 to select the first element of the list. # pop first element from a list colors = ['blue', 'green', 'red', 'yellow'] color = colors.pop(0) print(color) In this example, the list colors contains the 4 colors: blue, green, red and yellow. The list.pop() method is called on the colors list and the index 0 is passed to pop the first element of the list. Then the first color of the list is printed and the result is blue as shown below. blue

last element of list using list.pop()

To pop the last element of a list, use the list.pop() method without passing any argument. The default value of the index parameter is -1, so the last element of the list is selected. # pop last element from a list colors = ['blue', 'green', 'red', 'yellow'] color = colors.pop() print(color) In this example, the list colors contains the 4 colors: blue, green, red and yellow (exactly like in the example above). Then, the list.pop() method is called on the colors list without any argument, the index parameter defaults to -1. This selects the last element of the list, which is the string value 'yellow', and assigns it to the variable color. Then color is printed and the result is yellow as shown below. The last element is removed from the list by the list.pop() method. yellow

element by index using list.pop()

To select a specific element from a list, use the list.pop() method and pass the position as index parameter as shown below. # pop specific element by index from a list colors = ['blue', 'green', 'red', 'yellow'] color = colors.pop(2) print(color) In this example, the list colors contains the four colors: blue, green, red, and yellow (exactly like in the other examples above). Then, the list.pop() method is called on the colors list. The position of the elements to be returned is passed as an argument. This example selects the third element in the colors list by passing the index 2 to the pop() method. The returned color is assigned to the variable color and then printed. The result is the string value 'red', which is also removed from the list by the list.pop() method. red

multiple elements of list using list.pop()

To pop multiple elements from a list, call the list.pop() method multiple times as shown below. # pop multiple elements from a list colors = ['blue', 'green', 'red', 'yellow'] color1 = colors.pop(0) color2 = colors.pop(0) print(color1) print(color2) In this example, the list colors contains the four colors: blue, green, red, and yellow (exactly like in the other examples above). Then, the list.pop() method is called twice, each time with the index 0 to pop the first element of the list. The results are assigned to the variables color1 and color2. The color1 variable contains the string 'blue' because it is the first element of the list. After 'blue' is assigned to the variable, it is removed from the list by the pop() method. Then, the pop() method is called again with index 0, and once again the first element is returned. The 'blue' element has been removed by the last call of list.pop() and therefore 'green' is now the first element of the list, which is assigned to the variable color2. Finally, both variables are printed. blue green To automate the calls to the list.pop() method, use loops or list comprehension. # pop multiple elements from a list using list comprehension colors = ['blue', 'green', 'red', 'yellow'] select_indexes = [0, 0, -1] selected_colors = [colors.pop(index) for index in select_indexes] print(selected_colors) In this example, the list colors contains the four colors: blue, green, red, and yellow (exactly like in the other examples above). Then, a list containing the indexes 0, 0 and -1 is assigned to the variable select_indexes. Then list comprehension is used to apply the list.pop() method for every index defined and the results are saved in the list selected_colors. Finally, the selected_colors are printed and the list contains the values blue, green and yellow. Based on the indexes defined in the select_indexes list the pop() method selected the first element, then again the first element and finally the last element of the colors list. ['blue', 'green', 'yellow']

pop vs. remove

The main difference between the list.pop() and the list.remove() method is that pop() returns the element before it is removed from the list. The remove() method just removes the element without returning it first. Another difference is that the pop() method uses indexes to remove elements, while the remove() method uses values. # pop uses indexes vs remove uses values list.pop(index) list.remove(value)

syntax of list.pop()

The syntax of the Python list.pop() method is: list.pop([index=-1])

arguments of list.pop()

The list.pop() methods accepts zero to one argument. The optional argument is the index of the element to be returned. If no argument is passed the last element of the list will be returned. If the index passed to the list.pop() method is out of range, an IndexError exception will be raised.
argument required default value description
index optional -1 the index of the element to be returned and removed
IndexError: pop index out of range To prevent IndexErrors, use the built-in len() function. It returns the length of the list, which is the number of elements. Therefore, the maximum index is the length -1 (because Python indexes begin at 0 while length begins at 1) and the minimum index is 0.

return value of list.pop()

The list.pop() method returns the element at the given index. The element is then removed from the list. If no argument is passed, the last element of the list is returned.

time complexity of list.pop()

The time complexity of list.pop() is O(1) for the last element of the list (the default value of the index parameter). And the time complexity is O(n) for a specific element in the list because the whole list has to be shifted. All indexes are reassigned after the element is removed from it.

video explanation

The following video gives a quick overview of the list.pop() method and demonstrates how to use it.

related to list.pop()

The remove() method is similar to the pop() method, because it also removes the element. However, it works with values instead of indexes and doesn't return the element before removing it.

                
        

Summary


Click to jump to section