In Python, the pop function is employed to eliminate the item from a list located at a specified index. This function also provides the value of the item that has been removed. In cases where the index is not provided, it defaults to removing and returning the final element of the list.
This approach is particularly useful when there is a need to dynamically alter a list, as it directly changes the original list.
Syntax:
pop([idx])
Parameter(s):
- idx (optional): This parameter indicates the specific element that is intended to be removed from the list. By default, it is set to -1, which implies that the function will return the final element of the list.
- The element that has been removed is returned.
Examples of List pop
We will now examine several instances of the list pop method in Python:
Example 1: Simple Use Case of the pop Method
Let’s examine a straightforward illustration to grasp how the list pop method operates.
Example
# Simple Example of list pop() Method
# given list
lst_1 = [19, 3, 14, 7, 22, 5]
print("Given List:", lst_1)
print()
# using pop() method to remove and return the element
print("Popped Element:", lst_1.pop()) # since no argument is defined, it will pop last element
print("List after Popping:", lst_1)
print()
# using pop() method again
print("Popped Element:", lst_1.pop())
print("List after Popping:", lst_1)
Output:
Given List: [19, 3, 14, 7, 22, 5]
Popped Element: 5
List after Popping: [19, 3, 14, 7, 22]
Popped Element: 22
List after Popping: [19, 3, 14, 7]
Explanation:
In this illustration, we have a list that contains several elements. We utilized the pop function to eliminate one of the elements. Because we did not indicate an index value, the last element in the list will be removed. We repeated this operation once more.
Consequently, each time the pop method is invoked, it removes the final element from the list.
Example 2: Popping the Specific Element from the List
Let’s examine an example to grasp the process of eliminating a particular element from a list.
Example
# Simple Example of list pop() Method
# given list
lst_1 = [19, 3, 14, 7, 22, 5]
print("Given List:", lst_1)
print()
# using pop() method to remove and return the element
print("Popped Element:", lst_1.pop(2)) # since index=2 is specified, the element 14 will be popped
print("List after Popping:", lst_1)
print()
# using pop() method again
print("Popped Element:", lst_1.pop(0)) # index=0, element = 19 popped
print("List after Popping:", lst_1)
Output:
Given List: [19, 3, 14, 7, 22, 5]
Popped Element: 14
List after Popping: [19, 3, 7, 22, 5]
Popped Element: 19
List after Popping: [3, 7, 22, 5]
Explanation:
In this illustration, we are presented with a collection that contains various elements. We utilized the pop function to eliminate specific elements by indicating their index positions, such as 2 and 0.
Consequently, the values 14 and 19 have been removed from the provided list.
Example 3: Removing the Elements from the Last
Elements can be eliminated from the end of a list by using a negative integer to indicate the index value. Consider the following example:
Example
# Simple Example of list pop() Method
# given list
lst_1 = [19, 3, 14, 7, 22, 5]
print("Given List:", lst_1)
print()
# using pop() method to remove and return the element
print("Popped Element:", lst_1.pop(-1)) # since index=-1 is specified, the element 5 will be popped
print("List after Popping:", lst_1)
print()
# using pop() method again
print("Popped Element:", lst_1.pop(-4)) # index=-4, element = 3 popped
print("List after Popping:", lst_1)
Output:
Given List: [19, 3, 14, 7, 22, 5]
Popped Element: 5
List after Popping: [19, 3, 14, 7, 22]
Popped Element: 3
List after Popping: [19, 14, 7, 22]
Explanation:
In this illustration, we have a collection that contains several elements. We employed the pop method to eliminate specific elements by indicating the index values of -1 and -4.
Consequently, the values 5 and 3 have been removed from the provided list.
Conclusion
The pop function serves as an essential method for lists. It enables developers to eliminate an element from the list by indicating its index number. In cases where the user does not provide a specific index value, the pop function will, by default, remove the final element from the list. This tutorial has provided an overview of this method, including its syntax, along with several examples designed to enhance your comprehension of this functionality.
The pop method in Python serves a specific purpose within data structures such as lists and dictionaries. Its primary function is to remove and return an element from a given index in a list, or to delete a specified key in a dictionary, while also returning the associated value.
Here’s a more detailed explanation of its uses:
- In Lists: When applied to a list, the pop method retrieves and eliminates the element located at the specified index. If no index is indicated, it defaults to removing and returning the last item in the list. This feature is particularly beneficial for managing collections of data where the last-in, first-out (LIFO) principle is needed.
Example:
my_list = [10, 20, 30, 40]
last_element = my_list.pop() # Removes and returns 40
print(my_list) # Output: [10, 20, 30]
print(last_element) # Output: 40
- In Dictionaries: When used with a dictionary, the pop method allows for the removal of a specified key, returning its corresponding value. If the key does not exist, it raises a KeyError unless a default value is provided.
Example:
my_dict = {'a': 1, 'b': 2, 'c': 3}
value = my_dict.pop('b') # Removes the key 'b' and returns 2
print(my_dict) # Output: {'a': 1, 'c': 3}
print(value) # Output: 2
In summary, the pop method is an efficient way to manage and manipulate elements in both lists and dictionaries, providing a straightforward means of removing and accessing data.
The pop function eliminates the element located at the specified index in the list and also returns that element.
The syntax is given below:
The pop function will yield the last element of the list when no argument is supplied for the index parameter.
In the absence of a specified value for the index parameter, the pop method will, by default, eliminate the final item from the list.
Example
list = [1, 2, 3]
print("The removed value is",list.pop())
Output:
The removed value is 3
- Is it possible to use a negative index as a parameter when calling the pop method?
Indeed, when a negative index is provided, the pop function will eliminate the final element from the list.
Example
list = [1, 2, 3]
print("The value is ",list.pop(-1))
Output:
The value is 3
- What occurs when we supply an index that exceeds the boundaries of the specified list?
When the specified index exceeds the bounds of the list, invoking the pop method will result in an error, specifically an IndexError.
Example
list = [1, 2, 3]
print("The value is ",list.pop(10))
Output:
Traceback (most recent call last):
File "./prog.py", line 2, in <module>
IndexError: pop index out of range