{"id":4742,"date":"2017-12-07T00:42:21","date_gmt":"2017-12-06T19:12:21","guid":{"rendered":"https:\/\/www.java2blog.com\/?p=4742"},"modified":"2023-11-25T17:07:43","modified_gmt":"2023-11-25T11:37:43","slug":"python-list-remove","status":"publish","type":"post","link":"https:\/\/java2blog.com\/python-list-remove\/","title":{"rendered":"Python List remove()"},"content":{"rendered":"<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=\"#Python_List_remove_syntax\">Python List remove syntax<\/a><\/li><li><a href=\"#Python_List_remove_example\">Python List remove example<\/a><\/li><\/ul><\/div>\n<p>In this tutorial, we will see about <a href=\"https:\/\/www.java2blog.com\/python-list-tutorial\/\" target=\"_blank\" rel=\"noopener\">Python List<\/a>&#8216;s remove method.Python List remove method is used to remove passed element from the list.It will remove first matching element from the list.<\/p>\n<h2><span id=\"Python_List_remove_syntax\"><span style=\"color: #f89820;\">Python List remove syntax<\/span><\/span><\/h2>\n<pre class=\"python\" name=\"code\">\nlist1.remove(element)\n<\/pre>\n<p>here list1 is object of list.<\/p>\n<h2><span id=\"Python_List_remove_example\"><span style=\"color: #f89820;\">Python List remove example<\/span><\/span><\/h2>\n<p>You can simply call remove method to delete an element from the list.<br \/>\nLet&#8217;s understand this with the help of simple example.<\/p>\n<pre code=\"python\">\nlistOfCars=['Honda','Hyundai','Maruti','Suzuki']\nlistOfCars.remove('Maruti')\nprint(\"listOfCar:\",listOfCars)\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"content-box-green\">\nlistOfCar: [&#8216;Honda&#8217;, &#8216;Hyundai&#8217;, &#8216;Suzuki&#8217;]\n<\/div>\n<p>As you can see here, &#8216;Maruti&#8217; got removed from the list.<br \/>\nWhat if passed element is not in the list?<br \/>\nIf element is not in the list, then remove method will raise ValueError.<\/p>\n<pre code=\"python\">\nlistOfCars=['Honda','Hyundai','Maruti','Suzuki']\nlistOfCars.remove('Punto')\nprint(\"listOfCar:\",listOfCars)\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"content-box-green\">\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-397-23dc87563792> in <module>()<br \/>\n      1 listOfCars=[&#8216;Honda&#8217;,&#8217;Hyundai&#8217;,&#8217;Maruti&#8217;,&#8217;Suzuki&#8217;]\n&#8212;-&gt; 2 listOfCars.remove(&#8216;Punto&#8217;)<br \/>\n      3 print(&#8220;listOfCar:&#8221;,listOfCars)<\/p>\n<p>ValueError: list.remove(x): x not in list<br \/>\n<\/module><\/ipython-input-397-23dc87563792><\/div>\n<p>That\u2019s all about Python List remove method. Also, check <a href=\"https:\/\/java2blog.com\/remove-last-element-from-list-python\/\" title=\"how to remove last element from list in python\">how to remove last element from list in python<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Table of ContentsPython List remove syntaxPython List remove example In this tutorial, we will see about Python List&#8216;s remove method.Python List remove method is used to remove passed element from the list.It will remove first matching element from the list. Python List remove syntax list1.remove(element) here list1 is object of list. Python List remove example [&hellip;]<\/p>\n","protected":false},"author":31,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_mi_skip_tracking":false},"categories":[146,145],"tags":[],"_links":{"self":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/4742"}],"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\/31"}],"replies":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/comments?post=4742"}],"version-history":[{"count":1,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/4742\/revisions"}],"predecessor-version":[{"id":25892,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/4742\/revisions\/25892"}],"wp:attachment":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/media?parent=4742"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/categories?post=4742"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/tags?post=4742"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}