{"id":7292,"date":"2019-04-01T23:52:18","date_gmt":"2019-04-01T18:22:18","guid":{"rendered":"https:\/\/java2blog.com\/?p=7292"},"modified":"2023-11-25T16:24:12","modified_gmt":"2023-11-25T10:54:12","slug":"remove-from-list-python","status":"publish","type":"post","link":"https:\/\/java2blog.com\/remove-from-list-python\/","title":{"rendered":"Remove from list Python"},"content":{"rendered":"<p>In this post, we will see how to remove an element from list in python.<\/p>\n<p>You can remove elements from list in 3 ways.<br \/>\n<div id=\"toc_container\" class=\"toc_light_blue no_bullets\"><p class=\"toc_title\">Table of Contents<\/p><ul class=\"toc_list\"><li><a href=\"#Using_list_object8217s_remove_method\">Using list object&#8217;s remove method<\/a><\/li><li><a href=\"#Using_list_object8217s_pop_method\">Using list object&#8217;s pop method<\/a><\/li><li><a href=\"#Using_operator_del\">Using operator del<\/a><\/li><\/ul><\/div>\n\n<hr\/>\n<h2><span id=\"Using_list_object8217s_remove_method\"><span style=\"color: #f89820;\">Using list object&#8217;s remove method<\/span><\/span><\/h2>\n<p>Here you need to specify element which you want to remove. If there are multiple occurrences of element, then first occurrence will be removed.<\/p>\n<p>Please note that this is quite slow as it searches for element in the list and then remove the element by its value.<br \/>\nIf the element is not present in the list, then it raises ValueError.<\/p>\n<pre code=\"python\">\nlist1=[\"India\", 1, \"Nepal\", 2,5,1,2,3,4,3,2]\nlist1.remove(2)\nprint(list1)\n\nlist1.remove(9)\nprint(list1)\n<\/pre>\n<div class=\"content-box-green\">\n[&#8216;India&#8217;, 1, &#8216;Nepal&#8217;, 5, 1, 2, 3, 4, 3, 2]\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br \/>\nValueError                                Traceback (most recent call last)<br \/>\n<ipython-input-16-8d60d1ac6f32> in <module>()<br \/>\n      3 print(list1)<br \/>\n      4<br \/>\n&#8212;-&gt; 5 list1.remove(9)<br \/>\n      6 print(list1)<\/p>\n<p>ValueError: list.remove(x): x not in list<br \/>\n<\/module><\/ipython-input-16-8d60d1ac6f32><\/div>\n<hr\/>\n<h2><span id=\"Using_list_object8217s_pop_method\"><span style=\"color: #f89820;\">Using list object&#8217;s pop method<\/span><\/span><\/h2>\n<p>This method takes index of element as input and remove it. It throws IndexError if index is out of range.<\/p>\n<pre code=\"python\">\nlist1=[\"India\", 1, \"Nepal\", 2,5,1,2,3,4,3,2]\nlist1.pop(2)\nprint(list1)\n\nlist1.pop(14)\nprint(list1)\n<\/pre>\n<div class=\"content-box-green\">\n[&#8216;India&#8217;, 1, 2, 5, 1, 2, 3, 4, 3, 2]\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br \/>\nIndexError                                Traceback (most recent call last)<br \/>\n<ipython-input-17-9ee6e06bab31> in <module>()<br \/>\n      3 print(list1)<br \/>\n      4<br \/>\n&#8212;-&gt; 5 list1.pop(14)<br \/>\n      6 print(list1)<\/p>\n<p>IndexError: pop index out of range<br \/>\n<\/module><\/ipython-input-17-9ee6e06bab31><\/div>\n<hr\/>\n<h2><span id=\"Using_operator_del\"><span style=\"color: #f89820;\">Using operator del<\/span><\/span><\/h2>\n<p>This operator takes index of element as a input and remove the element. del operator also supports removing range of elements from the list.This is clear and fast way of removing elements from the list.<\/p>\n<pre code=\"python\">\nlist1=[\"India\", 1, \"Nepal\", 2,5,1,\"France\",2,3,4,3,2]\ndel list1[2]\nprint(list1)\n\ndel list1[4:7]\nprint(list1)\n<\/pre>\n<div class=\"content-box-green\">\n[&#8216;India&#8217;, 1, 2, 5, 1, &#8216;France&#8217;, 2, 3, 4, 3, 2]\n[&#8216;India&#8217;, 1, 2, 5, 3, 4, 3, 2]\n<\/div>\n<p>That&#8217;s all about Python remove from list.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Table of ContentsUsing list object&#8217;s remove methodUsing list object&#8217;s pop methodUsing operator del In this post, we will see how to remove an element from list in python. You can remove elements from list in 3 ways. Using list object&#8217;s remove method Here you need to specify element which you want to remove. If there [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_mi_skip_tracking":false},"categories":[145,146],"tags":[],"_links":{"self":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/7292"}],"collection":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/comments?post=7292"}],"version-history":[{"count":1,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/7292\/revisions"}],"predecessor-version":[{"id":25868,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/7292\/revisions\/25868"}],"wp:attachment":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/media?parent=7292"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/categories?post=7292"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/tags?post=7292"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}