Python pop () Function
In this tutorial, we will study what a Pop function in Python is, How it works and look into some examples of pop function in a list.
Pop() Function in a list in Python
What is Pop Function
Pop is an inbuilt function in python which removes the last element from the list for eg. mylist = [1,2,3,4,5] on executing the mylist.pop() the last element i.e 5 is removed from the list and we get [1,2,3,4] as output.
- list_name is the name of your list
- pop is the inbuilt function used to delete the element
- index is the position of the element to be removed, it can also be left empty (by default it will delete the last element)
- pop function returns updated list after deletion.
list = ['code', 'speedy', '.', 'com'] #Case 1 list1.pop() print(list) #Case 2 list1.pop() print(list) #Case 3 list1.pop(0) print(list)
['code', 'speedy', '.'] ['code', 'speedy'] ['speedy']
- First, we need to declare a list on which we can operate.
- The first pop function deletes the last element i.e ‘com’
- Now ‘.’ is the last element and when we run the pop command the last element is deleted
- And finally, when we run the pop(0) the element in index 0 is deleted i.e ‘code’