Pop() in Python

Overview

The Python pop() function deletes an element from a list at an index or if no index is specified, the last item. It comes in handy for removing items, from a list without changing the order of elements. What sets pop() apart is its capacity to make changes directly to the list removing elements without the necessity of generating a list.

Functionality

If you don't mention an index when using pop() it will take out the final element in the list. This can come in handy when the order of elements doesn't matter much. Yet be careful as pop() will show an error if the index is, beyond the lists limits. To prevent this error make sure that the index you pick falls within the range of the list.

Definition of Pop Function in Python

Basic Definition

The pop() method, in Python is a built in function that is utilized to delete an element from a list at an index. Here is how you can use it

list.pop(index)

If no index is specified, pop() removes the last element. The function returns the removed item, allowing further operations with it if needed.

Syntax Examples

fruits = ['apple', 'banana', 'orange', 'grape']
removed_fruit = fruits.pop()
print(removed_fruit)  # Output: grape
print(fruits)  # Output: ['apple', 'banana', 'orange']

Removing Last Element

fruits = ['apple', 'banana', 'orange', 'grape']
removed_fruit = fruits.pop()
print(removed_fruit)  # Output: grape
print(fruits)  # Output: ['apple', 'banana', 'orange']

Removing Specific Element

fruits = ['apple', 'banana', 'orange', 'grape']
removed_fruit = fruits.pop(2)
print(removed_fruit)  # Output: orange
print(fruits)  # Output: ['apple', 'banana', 'grape']

Purpose of Using Pop Function in Python

The pop() method is often utilized to delete an item from a list using its index position. It also gives back the deleted item. This feature comes in handy in scenarios involving data structures that require frequent addition and removal of elements.

Syntax of Pop Function

The syntax of the pop() function is as follows:

list_name.pop(index)

  • list_name: The name of the list from which you want to remove an item.
  • index: The position of the item you intend to remove. The index starts from 0.

If no argument is given, pop() removes the last item from the list.

Optional Parameters in Pop Function

The pop() function usually needs an index as its input. Some programming languages might offer optional parameters, for added versatility and features.

Usage of Pop Function with Lists

Removing Last Item

fruits = ["apple", "banana", "orange"]
last_fruit = fruits.pop()
print(last_fruit)  # Output: orange
print(fruits)  # Output: ["apple", "banana"]

Removing Item at Specific Index

fruits = ["apple", "banana", "orange"]
second_fruit = fruits.pop(1)
print(second_fruit)  # Output: banana
print(fruits)  # Output: ["apple", "orange"]

Removing Elements from a List Using Pop Function

Specific Index

my_list = [1, 2, 3, 4, 5]
my_list.pop(2)
print(my_list)  # Output: [1, 2, 4, 5]

Last Element

my_list = [1, 2, 3, 4, 5]
my_list.pop()
print(my_list)  # Output: [1, 2, 3, 4]

Negative Indices and Their Role in Pop Function

Negative indices allow access to elements from the end of the list. For example, my_list.pop(-1) removes and returns the last item, while my_list.pop(-2) removes and returns the second last item.

Default List Behavior with Pop Function

The pop() function, in a list removes the element causing the lists length to decrease by one.

Example

my_list = [1, 2, 3, 4, 5]
print(my_list.pop())  # Output: 5
print(my_list)  # Output: [1, 2, 3, 4]

Restoring Original List After Removals with Pop Function

To restore the original list after using pop(), store removed elements in another list and re-insert them in the original list when needed.

original_list = [1, 2, 3, 4, 5]
removed_elements = []
# Remove elements
removed_elements.append(original_list.pop())
removed_elements.append(original_list.pop(0))
# Restore elements
for element in reversed(removed_elements):
    original_list.append(element)
print(original_list)  # Output: [3, 4, 5, 1, 5]

Create a free account to access the full topic

“It has all the necessary theory, lots of practice, and projects of different levels. I haven't skipped any of the 3000+ coding exercises.”
Andrei Maftei
Hyperskill Graduate