{"id":24707,"date":"2021-11-02T09:43:57","date_gmt":"2021-11-02T04:13:57","guid":{"rendered":"https:\/\/python-programs.com\/?p=24707"},"modified":"2021-11-05T17:21:02","modified_gmt":"2021-11-05T11:51:02","slug":"python-list-pop-method-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-list-pop-method-with-examples\/","title":{"rendered":"Python List pop() Method with Examples"},"content":{"rendered":"
In the previous article, we have discussed Python List insert() Method with Examples<\/a> Lists in Python are mutable sequences. They are extremely similar to tuples, except they do not have immutability constraints. Lists are often used to store collections of homogeneous things, but there is nothing stopping you from storing collections of heterogeneous items as well.<\/p>\n List pop() Method in Python:<\/strong><\/p>\n The pop() method removes the item at the specified index from the list and returns the item that was removed.<\/p>\n Note:<\/strong> By default, it removes the last element from the list.<\/p>\n Syntax:<\/strong><\/p>\n Parameters<\/strong><\/p>\n The pop() method only accepts one argument (index).<\/p>\n index: <\/strong>This is optional.<\/p>\n Return Value:<\/strong><\/p>\n The pop() method returns the item at the specified index. This item has been removed from the list as well.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Know about the syntax and usage of functions to manipulate lists with the provided Python List Method Examples<\/a> and use them.<\/p>\n In the previous article, we have discussed Python List insert() Method with Examples List in Python: Lists in Python are mutable sequences. They are extremely similar to tuples, except they do not have immutability constraints. Lists are often used to store collections of homogeneous things, but there is nothing stopping you from storing collections of …<\/p>\n
\nList in Python:<\/strong><\/p>\nlist.pop(index)<\/pre>\n
\n
Given list = ['good', 'morning', 'btechgeeks', 4, 5, 6]\r\nGiven index = 2<\/pre>\n
The value returned is: btechgeeks\r\nThe given list after poping 2 item = ['good', 'morning', 4, 5, 6]<\/pre>\n
Given list = ['hello', 'this', 'is', 'btechgeeks']<\/pre>\n
The value returned is: btechgeeks\r\nThe given list after poping with giving an index is : ['hello', 'this', 'is']<\/pre>\n
List pop() Method with Examples in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Give the list as static input and store it in a variable.\r\ngvn_lst = ['good', 'morning', 'btechgeeks', 4, 5, 6]\r\n# Give the number(index) as static input and store it in another variable.\r\ngvn_indx = 2\r\n# Pass the given index as a parameter to the pop() function for the given\r\n# list (which removes the element at the given index and returns the value\r\n# that is removed).\r\n# Store it in another variable.\r\nretrnvalu = gvn_lst.pop(gvn_indx)\r\n# Print the above-returned value.\r\nprint(\"The value returned is: \", retrnvalu)\r\n# Print the given list after poping the item at the given index value.\r\nprint(\"The given list after poping\", gvn_indx, \"item = \", gvn_lst)\r\n<\/pre>\n
The value returned is: btechgeeks\r\nThe given list after poping 2 item = ['good', 'morning', 4, 5, 6]<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Give the list as user input using list(),map(),input(),and split() functions.\r\n# Store it in a variable.\r\ngvn_lst = list(map(int, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\n# Give the number(index) as user input using the int(input()) function \r\n# and store it in another variable.\r\ngvn_indx = int(input(\"Enter some random number = \"))\r\n# Pass the given index as a parameter to the pop() function for the given\r\n# list (which removes the element at the given index and returns the value\r\n# that is removed).\r\n# Store it in another variable.\r\nretrnvalu = gvn_lst.pop(gvn_indx)\r\n# Print the above-returned value.\r\nprint(\"The value returned is: \", retrnvalu)\r\n# Print the given list after poping the item at the given index value.\r\nprint(\"The given list after poping\", gvn_indx, \"item = \", gvn_lst)\r\n<\/pre>\n
Enter some random List Elements separated by spaces = 25 60 56 13 12\r\nEnter some random number = 3\r\nThe value returned is: 13\r\nThe given list after poping 3 item = [25, 60, 56, 12]<\/pre>\n
Similarly, pop without an index and negative index<\/h3>\n
\n
# Give the list as static input and store it in a variable.\r\ngvn_lst = ['hello', 'this', 'is', 'btechgeeks']\r\n# Give the negative number(index) as static input and store it in\r\n# another variable.\r\ngvn_indx = -3\r\n# Pass the given index as a parameter to the pop() function for the given\r\n# list (which removes the element at the given index and returns the value\r\n# that is removed).\r\n# Store it in another variable.\r\nretrnvalu = gvn_lst.pop(gvn_indx)\r\n# Print the above-returned value.\r\nprint(\"The value returned is: \", retrnvalu)\r\n# Print the given list after poping the item at the given index value.\r\nprint(\"The given list after poping\", gvn_indx, \"item = \", gvn_lst)\r\n# Apply pop() function to the given list without giving an index and get the\r\n# value that is returned.\r\n# Store it in another variable.\r\nretrnvalu1 = gvn_lst.pop()\r\n# Print the above-returned value.\r\nprint(\"The value returned is: \", retrnvalu1)\r\n# Print the given list after applying pop() function\r\nprint(\"The given list is after applying pop() function:\", gvn_lst)\r\n<\/pre>\n
The value returned is: this\r\nThe given list after poping -3 item = ['hello', 'is', 'btechgeeks']\r\nThe value returned is: btechgeeks\r\nThe given list is after applying pop() function: ['hello', 'is']<\/pre>\n
\n