Introduction to pop() in Python
pop() is a method of the complex datatype called list. The list is among the most commonly used complex datatype in python, and the pop() method is responsible for popping an item from the python list. The pop method will remove an item from a given index of the list and returns the removed item. So, in the end, the pop() method will be like a list without the element that was popped.
Syntax:
list.pop(index)
The index placed in the method is used to represent the specific element that needs to be popped out of the list. When some element is placed in the list’s argument in the case where that specific element is removed, considering a case where no specific index is specified, it pops the last element in the list. So the understanding is that the index value which is appended when no argument is passed on will be -1, which represents the last element in the list.
How does pop() Function work?
- The pop() will be accepting only one argument for its execution. This argument will be representing the index of the element, which is expected to be popped out from the specified list.
- The process of passing an argument to the list is very much optional; when no argument has been passed, then it represents the value of ‘-1’ as the default. Here basically, the default value of -1 represents the last position in the list.
- The outcome of the pop() method is the item placed at that specific position and along with the item being removed from that specific list.
- When the index value which has been passed is out of the range of the list index, then the ” IndexError: pop index out of rangeexception” will be thrown by the pop() method. So this means the pop() method will not work when the index is out of the index range.
- The key process performed in the backend during this index removal process is as listed, updating the index of the list, removing an element from the index, updating the remaining elements in the index, and removing elements from the index at the reverse of the list.
Examples of pop() in Python
Given below are the examples:
Example #1
Code:
# List of countries
Country = ['India','Australia','China','Greece','Italy']
# Return the 3rd item along with a removal
returned_Country_value = Country.pop(2)
print('Returned country Value:', returned_Country_value)
# Updated List
print('Updated country List:',Country)
Output:
Explanation:
- A couple of country names are declared as items of a list.
- The third item, namely the county china, is expected to be removed from the list from this list of items.
- This item is achieved by the use of the pop() function by holding the index value of 3 in it.
- The returned value and the updated values are printed in the console.
Example #2
Code:
# List provided
List = [11,3,211,6444,12,26]
# Before Pop operation of the list
print("List Before applying pop() method:", List)
# Poping the third item from the given list
List.pop(2)
# Poping the fifth item from the given list
List.pop(4)
# After Pop operation of the list
print("List After applying pop() method:", List)
Output:
Explanation:
- Here it is an integer list that holds a list of all integer values in it.
- The list has initially been printed in the console to display the original list, which is without any pop operation being performed.
- Then third and fourth items from the list are popped out, and the resulting list is again displayed in the console after the pop operation is performed.
Example #3
Code:
# List provided
List = [11,'c#',3,211,'python','java',6444,12,26]
# Before Pop operation of the list
print("List Before applying pop() method:\n", List)
# Poping the third item from the given list
List.pop(2)
# Poping the fifth item from the given list
List.pop(4)
# After Pop operation of the list
print("List After applying pop() method:\n", List)
Output:
Explanation:
- This example is again very similar to the above-given example; the only difference is here the list is a combination of both the integer and string values.
- The list has initially been printed in the console to display the original list, which is without any pop operation being performed.
- Then third and fourth items from the list are popped out, and the resulting list is again displayed in the console after the pop operation is performed.
Example #4
Code:
# List of countries
Country = ['India','Australia','China','Greece','Italy']
# Return the 3rd item along with a removal
returned_Country_value = Country.pop(7)
print('Returned country Value:', returned_Country_value)
# Updated List
print('Updated country List:',Country)
Output:
Explanation:
- The above example depicts the exception of ‘pop index out of range error”, the idea is like we have used a list that holds only around 4 valid index values in it, so with holding four values in it, the pop method is called for the 7th index value.
- Since the seventh values are not in the given range of the indexes, the “index out of range” exception has been triggered and printed in the console.
- This is actually a cooked up exception creation kind of an example.
Example #5
Code:
# List of countries
Country = ['India','Australia','China','Greece','Italy']
# Return the 3rd item along with a removal
returned_Country_value = Country.pop()
print('Returned country Value:', returned_Country_value)
# Updated List
print('Updated country List:',Country)
Output:
Explanation:
- The given example shows the use of the pop element in the python list without specifying the index value in the method arguments, so this means none of the index are mentioned so the pop automatically assigns a value of -1, which represents the last element in the list.
- So from out given list [‘India’, ‘Australia’, ‘China’,’Greece’,’Italy’], the final element is Italy.
- So Italy’s value will be removed from the given list and returned as the pop() items value.
- So the returned item along with the manipulated list is printed in the console.
Conclusion
Already python offers a classified list of collection data types, and the efficiency of each of the data types are widely incremented by means of functions like these. They offer greater and superior flexibility in the programming means.
Recommended Articles
This is a guide to pop() in Python. Here we discuss the introduction to pop() in python with appropriate syntax and respective examples. You may also have a look at the following articles to learn more –
40 Online Courses | 13 Hands-on Projects | 215+ Hours | Verifiable Certificate of Completion
4.8
View Course
Related Courses