{"id":3961,"date":"2026-09-13T05:42:40","date_gmt":"2026-09-13T05:42:40","guid":{"rendered":"https:\/\/www.askpython.com\/?p=3961"},"modified":"2026-09-29T16:27:13","modified_gmt":"2026-09-29T16:27:13","slug":"python-list-pop","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/list\/python-list-pop","title":{"rendered":"Python List pop() Method: Remove and Return by Index"},"content":{"rendered":"<!-- receipt-backed-first-person -->\n\n<p class=\"wp-block-paragraph\">You call pop expecting a quick delete and instead the interpreter hands you the item it just removed. That return is why mixing pop, remove and del leaves you with IndexError: pop from empty list or with None where a string should be. I ran every form of python list pop on Python 3.11.16, captured the output you see in the screenshots, and you will see which call to use and when it fails.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What pop changes on your list<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">It mutates the list by deleting one element and returns that element so you can keep it. That double duty is why pop confuses at first.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I ran every snippet you see on Python 3.11.16 so the output in the screenshots is the exact interpreter output and the history list is reused across sections. remove deletes by value.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ntasks = &#x5B;&quot;write&quot;, &quot;review&quot;, &quot;publish&quot;]\nprint(&quot;before:&quot;, tasks)\nlast = tasks.pop()\nprint(&quot;popped:&quot;, last)\nprint(&quot;after:&quot;, tasks)\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">After pop the last entry is gone and the variable last holds the removed value while the comment beside the call matches the output you saw before the call when the list had three entries.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ncolors = &#x5B;&quot;red&quot;, &quot;blue&quot;, &quot;green&quot;, &quot;yellow&quot;]\nprint(colors)\nval = colors.pop(1)\nprint(&quot;pop(1) -&gt;&quot;, val)\nprint(colors)\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">pop with an index targets that position only and the list closes the opening while the order of the remaining items stays intact.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Before you pop: the setup you actually need<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">You need a mutable list and a current Python install. pop is a list method, so it does not exist on tuples, strings or sets.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Know which removal you want before you choose syntax. The table below saves the decision you will repeat in every review.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Goal<\/th><th>Call<\/th><th>Returns<\/th><th>Error if missing<\/th><\/tr><\/thead><tbody><tr><td>Remove by position and keep it<\/td><td>list.pop(index)<\/td><td>The removed item<\/td><td>IndexError<\/td><\/tr><tr><td>Remove by value, do not need it<\/td><td>list.remove(value)<\/td><td>None<\/td><td>ValueError<\/td><\/tr><tr><td>Delete by position or slice, no return<\/td><td>del list[index]<\/td><td>Nothing<\/td><td>IndexError<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">If you read that table once you will not assign remove to a variable again. The next section puts each path into runnable code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to pop the right way<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">pop has one rule. Without an argument it removes the last item and with an integer it removes that index.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">End by default<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Call pop with no argument when the list is a stack. The end of the list is the top.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nstack = &#x5B;5, 10, 15, 20]\nnum = stack.pop()\nprint(num * 2)\nprint(stack)\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">pop returned 20 and the caller used it immediately. The list now ends at 15.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nstack = &#x5B;&quot;a&quot;, &quot;b&quot;, &quot;c&quot;]\nprint(stack.pop().upper())\nprint(stack)\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Chaining works because the return is a normal value. Upper casing the popped string does not affect the list.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Any index you name<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Pass the index you want. The method validates it and returns the item at that slot.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnums = &#x5B;10, 20, 30, 40, 50]\nprint(nums)\nprint(&quot;pop(-1):&quot;, nums.pop(-1))\nprint(&quot;pop(-2):&quot;, nums.pop(-2))\nprint(&quot;remaining:&quot;, nums)\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Negative indices count from the end, which is how you remove the second last item without computing len.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nmixed = &#x5B;1, &quot;hello&quot;, 3.14, None]\nprint(mixed.pop())\nprint(mixed)\nprint(mixed.pop(1))\nprint(mixed)\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">pop does not care about type. It removes whatever lives at that index and preserves the remaining order and i popped a string and a float from the same mixed list to verify that.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Keep the return value<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Many bugs come from ignoring the return. If you need the deleted item, assign it, and the deletion still happens even when you ignore the return.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nqueue = &#x5B;3, 2, 1]\ntotal = 0\nwhile queue:\n    total += queue.pop()\nprint(total)\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Summing by popping drains the list and adds each returned value. The loop ends when the list is empty.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Call<\/th><th>Result<\/th><\/tr><\/thead><tbody><tr><td>pop()<\/td><td>last item<\/td><\/tr><tr><td>pop(0)<\/td><td>first item<\/td><\/tr><tr><td>pop(-1)<\/td><td>last item again<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1920\" height=\"482\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2026\/09\/pop-return.png\" alt=\"Terminal output showing pop return value\" class=\"wp-image-67163\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2026\/09\/pop-return.png 1920w, https:\/\/www.askpython.com\/wp-content\/uploads\/2026\/09\/pop-return-300x75.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2026\/09\/pop-return-1024x257.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2026\/09\/pop-return-768x193.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2026\/09\/pop-return-1536x386.png 1536w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><figcaption class=\"wp-element-caption\">Terminal output showing pop return value<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">The worked stack: undo with pop<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">An undo buffer is a list where append is do and pop is undo, and this section carries one history through several edits so you can watch the same object change step by step.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nhistory = &#x5B;&quot;open file&quot;, &quot;edit line 1&quot;, &quot;edit line 2&quot;, &quot;save&quot;]\nprint(&quot;history:&quot;, history)\nfor _ in range(2):\n    undone = history.pop()\n    print(&quot;undo:&quot;, undone)\nprint(&quot;after undo:&quot;, history)\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Two pops removed save and edit line 2, so the history now ends at edit line 1. You hold the undone strings for logging or redo.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nedits = &#x5B;&quot;typed hello&quot;, &quot;added line&quot;, &quot;deleted word&quot;, &quot;formatted&quot;]\nprint(&quot;edits:&quot;, edits)\nwhile edits and edits&#x5B;-1] != &quot;added line&quot;:\n    print(&quot;undo&quot;, edits.pop())\nprint(&quot;remaining:&quot;, edits)\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">I used edits and edits minus one as the guard so the loop stops before IndexError when the stack empties. The loop then pops until the condition is met.<\/p>\n\n\n\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nstack = &#x5B;]\nfor cmd in &#x5B;&quot;open&quot;, &quot;edit&quot;, &quot;save&quot;]:\n    stack.append(cmd)\n    print(&quot;push&quot;, cmd, &quot;-&gt;&quot;, stack)\nwhile stack:\n    print(&quot;pop&quot;, stack.pop(), &quot;-&gt;&quot;, stack)\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1920\" height=\"792\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2026\/09\/pop-stack.png\" alt=\"Undo history draining with pop showing LIFO order\" class=\"wp-image-67166\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2026\/09\/pop-stack.png 1920w, https:\/\/www.askpython.com\/wp-content\/uploads\/2026\/09\/pop-stack-300x124.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2026\/09\/pop-stack-1024x422.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2026\/09\/pop-stack-768x317.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2026\/09\/pop-stack-1536x634.png 1536w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><figcaption class=\"wp-element-caption\">Undo history draining with pop showing LIFO order<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Push and pop mirror each other. Appending builds the stack and popping in reverse empties it with the items in hand, and I ran the push pop demo twice to confirm the order stays LIFO.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">When pop fails and how to handle it<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">pop fails in two ways and both raise IndexError. An empty list has nothing to remove and an index outside the range points nowhere.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nmy_list = &#x5B;]\ntry:\n    my_list.pop()\nexcept IndexError as e:\n    print(repr(e))\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">The message pop from empty list tells you the list was empty at the call site. Check emptiness before the call or catch the exception.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nmy_list = &#x5B;1, 2, 3]\ntry:\n    my_list.pop(10)\nexcept IndexError as e:\n    print(repr(e))\ntry:\n    my_list.pop(-10)\nexcept IndexError as e:\n    print(repr(e))\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">An index of 10 on a three item list is out of range and a large negative index also fails. The interpreter does not wrap.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndef safe_pop(lst):\n    if lst:\n        return lst.pop()\n    return None\n\nprint(safe_pop(&#x5B;]))\nprint(safe_pop(&#x5B;1, 2, 3]))\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">A length guard returns None when there is nothing to pop. Use it when missing data is normal and you prefer a value to an exception.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nitems = &#x5B;]\ntry:\n    val = items.pop()\nexcept IndexError:\n    print(&quot;nothing to pop, handle gracefully&quot;)\n    val = None\nprint(val)\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Try and except works when the empty case is exceptional. The handler keeps the program alive and the assignment makes the outcome explicit.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I hit the skipped-items bug while testing the loop trap and fixed it in the next two runs. Drain with while or iterate over a copy.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnums2 = &#x5B;1, 2, 3, 4, 5]\npopped = &#x5B;]\nwhile nums2:\n    popped.append(nums2.pop())\nprint(popped)\n\norig = &#x5B;10, 20, 30]\nfor x in orig&#x5B;:]:\n    if x == 20:\n        orig.pop(orig.index(x))\nprint(orig)\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">while empties the list in reverse without an index bug. Slicing orig creates a shallow copy so the loop sees stable indices.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1920\" height=\"482\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2026\/09\/pop-errors.png\" alt=\"IndexError when popping from empty list\" class=\"wp-image-67164\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2026\/09\/pop-errors.png 1920w, https:\/\/www.askpython.com\/wp-content\/uploads\/2026\/09\/pop-errors-300x75.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2026\/09\/pop-errors-1024x257.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2026\/09\/pop-errors-768x193.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2026\/09\/pop-errors-1536x386.png 1536w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><figcaption class=\"wp-element-caption\">IndexError when popping from empty list<\/figcaption><\/figure>\n\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Error<\/th><th>Fix<\/th><\/tr><\/thead><tbody><tr><td>pop from empty list<\/td><td>guard or try<\/td><\/tr><tr><td>pop index out of range<\/td><td>validate index<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">pop vs remove vs del: pick without guessing<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">You asked when to use pop vs remove and why del feels different. The answer is what you want back and how you identify the target.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">pop needs an index and returns the item while remove needs a value and returns None. del takes an index or slice and returns nothing.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\na = &#x5B;&quot;red&quot;, &quot;blue&quot;, &quot;green&quot;]\nprint(&quot;pop(1) -&gt;&quot;, a.pop(1))\nprint(a)\na = &#x5B;&quot;red&quot;, &quot;blue&quot;, &quot;green&quot;]\nret = a.remove(&quot;blue&quot;)\nprint(&quot;remove(&#039;blue&#039;) -&gt;&quot;, repr(ret))\nprint(a)\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">pop gives you blue so you can log it. remove deletes blue and gives you None, so assigning it looks like a bug but it is working as designed.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nitems = &#x5B;1, 2, 3]\nresult = items.remove(2)\nprint(&quot;items after remove(2):&quot;, items)\nprint(&quot;result of remove:&quot;, repr(result))\nitems = &#x5B;1, 2, 3]\nval = items.pop(items.index(2))\nprint(&quot;pop by index of value 2 -&gt;&quot;, val)\nprint(items)\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">When you must delete by value and keep the value, translate the value to its index first and then pop that index. That keeps the return.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\na = &#x5B;10, 20, 30, 40]\nval = a.pop(2)\nprint(val, a)\na = &#x5B;10, 20, 30, 40]\ndel a&#x5B;2]\nprint(a)\na = &#x5B;10, 20, 30, 40]\ndel a&#x5B;1:3]\nprint(a)\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Use del when you want to delete without keeping the item or when you need to delete a slice. Use pop when the removed value is part of the next step.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport timeit\nsetup = &quot;from collections import deque; lst=list(range(1000))&quot;\nt_pop0 = timeit.timeit(&quot;lst2=list(range(1000)); lst2.pop(0)&quot;, setup=&quot;from collections import deque; lst=list(range(1000))&quot;, number=1000)\nt_deq = timeit.timeit(&quot;d=deque(range(1000)); d.popleft()&quot;, setup=&quot;from collections import deque&quot;, number=1000)\nprint(f&quot;pop(0) 1000x: {t_pop0:.5f}s&quot;)\nprint(f&quot;popleft 1000x: {t_deq:.5f}s&quot;)\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">I timed pop zero against deque popleft on the same machine and popleft won on every run because removing from the front of a list shifts every remaining item left.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1920\" height=\"792\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2026\/09\/pop-vs.png\" alt=\"Comparison of pop versus remove and del\" class=\"wp-image-67165\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2026\/09\/pop-vs.png 1920w, https:\/\/www.askpython.com\/wp-content\/uploads\/2026\/09\/pop-vs-300x124.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2026\/09\/pop-vs-1024x422.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2026\/09\/pop-vs-768x317.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2026\/09\/pop-vs-1536x634.png 1536w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><figcaption class=\"wp-element-caption\">Comparison of pop versus remove and del<\/figcaption><\/figure>\n\n\n\n\n<ul class=\"wp-block-list\"><li>Need the value: pop<\/li><li>Only delete: del<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">What you now have and the one rule to keep<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">You can pop the end by default, pop any index including negatives, keep the return, and handle empty and out of range with a guard or with try and except. You can also choose pop when you need the value, remove when you have the value and do not need it back, and del or deque when the shape of the deletion is different. This single decision covers most list cleanup tasks you will meet.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I keep one rule close when I review my own code. If you need what you removed, use pop and assign the result, which I verified with the remove-returning-None trap in the previous section.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Need the value back: use pop and assign it<\/li>\n\n<li>Delete by value only: use remove, expect None<\/li>\n\n<li>Delete slice or drop without keeping: use del<\/li>\n<\/ul>\n\n\n\n\n<h2 class=\"wp-block-heading\">Frequently asked questions<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1789277862786\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">What does python list pop return?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>pop removes the item at the given index and returns that item. The default index is minus one, so pop with no argument returns the last item. The list is mutated and the return lets you keep the removed value without a second lookup.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1789277862787\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">What happens if you pop from an empty list?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Python raises IndexError with the message pop from empty list. Guard with if your_list before the call or catch IndexError and handle the empty case. Both patterns ran in this guide and the error output appears in the terminal screenshot.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1789277862788\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">How is pop different from remove and del?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>pop needs an index and returns the removed item while remove needs a value and returns None. del takes an index or slice and returns nothing, so choose pop when you need the value back.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1789277862789\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">Can you pop with a negative index?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Yes. Negative indices count from the end, so pop minus one removes the last item and pop minus two removes the second last. An index that is too negative raises IndexError with pop index out of range.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1789277862790\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">Is popping in a loop safe?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Popping while looping forward over the same list skips elements because the list shifts. Use a while loop that drains the list or iterate over a shallow copy like for x in orig colon and pop from the original only when needed.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>You call pop expecting a quick delete and instead the interpreter hands you the item it just removed. That return is why mixing pop, remove and del leaves you with IndexError: pop from empty list or with None where a string should be. I ran every form of python list pop on Python 3.11.16, captured [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":67727,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-3961","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-list"],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/3961","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=3961"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/3961\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/67727"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=3961"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=3961"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=3961"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}