{"id":4787,"date":"2020-11-09T22:23:13","date_gmt":"2020-11-09T22:23:13","guid":{"rendered":"https:\/\/www.linuxscrew.com\/?p=4787"},"modified":"2022-06-23T22:13:34","modified_gmt":"2022-06-23T22:13:34","slug":"remove-items-from-list-python","status":"publish","type":"post","link":"https:\/\/www.linuxscrew.com\/remove-items-from-list-python","title":{"rendered":"How To Remove Items From A List in Python (With Examples)"},"content":{"rendered":"<p><a href=\"https:\/\/www.linuxscrew.com\/category\/programming\/python\">Python<\/a> is widely used for both scripting and automation on <a href=\"https:\/\/www.linuxscrew.com\/category\/linux\">Linux<\/a>, as well as building web and standalone applications.<\/p>\n<p>Python is designed to be easy to learn, easy to write, and easy to read. It&#8217;s a great multi-purpose programming language.<\/p>\n<p>Python has several different types of arrays for storing data.<\/p>\n<p><strong>Lists are <a href=\"https:\/\/www.linuxscrew.com\/javascript-array-shift\">arrays which allow you to store items.<\/a> Items in <a href=\"https:\/\/docs.python.org\/3\/library\/stdtypes.html#list\" target=\"_blank\" rel=\"noopener\">lists<\/a> can be altered, and are stored in a specific order.<\/strong><\/p>\n<h2>Syntax and Examples<\/h2>\n<p>There are several methods you can use to remove an item from a list in Python &#8211;\u00a0<strong>pop()<\/strong>,\u00a0<strong>del()<\/strong>, and <strong>remove()<\/strong>. Which one you use is based on what you want the resulting list to look like.<\/p>\n<h3>pop()<\/h3>\n<p>Removes the item at the given index in the list. Also returns the value at that index:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">list = [99, 32, 44, 66, 32]\r\npopped = list.pop(2)\r\nprint(popped) # 44\r\nprint(list) # [99, 32, 66, 32]<\/pre>\n<p>Note that:<\/p>\n<ul>\n<li>The\u00a0<em>index<\/em>\u00a0is the items position in the list. When dealing with indexes we start counting at\u00a0<strong>0<\/strong>, not\u00a0<strong>1<\/strong>! So the first item in the list is index 0, the second item is index 1, and so on<\/li>\n<li>Text following the # symbol is a comment<\/li>\n<li><a href=\"https:\/\/www.linuxscrew.com\/multi-line-comments-python\">Comments are ignored by Python when executing your code<\/a> &#8211; allowing you to include notes about what you&#8217;re doing<\/li>\n<\/ul>\n<h3>del()<\/h3>\n<p>Removes a <a href=\"https:\/\/www.linuxscrew.com\/python-dictionary-slice\">slice<\/a> from the list in the format\u00a0<em>start:end<\/em>, where\u00a0<em>start<\/em>\u00a0and\u00a0<em>end<\/em> are indexes of items in the list:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">list = [99, 32, 44, 66, 32]\r\ndel list[2:3]\r\nprint(list) # [99, 32, 32]<\/pre>\n<h3>remove()<\/h3>\n<p>Deletes the first instance of a value found in a list:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">list = [99, 32, 44, 66, 32]\r\nlist.remove(3:2)\r\nprint(list) # [99, 44, 66, 32]<\/pre>\n<h2>Conclusion<\/h2>\n<p>Removing items from lists is a vital component of any Python application. If you&#8217;re just getting started learning Python, <a href=\"https:\/\/www.linuxscrew.com\/category\/programming\/python\">check out our other tutorials<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python is widely used for both scripting and automation on Linux, as well as building web and standalone applications. Python is designed to be easy to learn, easy to write, and easy to read. It&#8217;s a great multi-purpose programming language. Python has several different types of arrays for storing data. Lists are arrays which allow you to store items. Items in lists can be altered, and are stored in a specific order. Syntax and Examples There are several methods you can use to remove an &#8230; <a title=\"How To Remove Items From A List in Python (With Examples)\" class=\"read-more\" href=\"https:\/\/www.linuxscrew.com\/remove-items-from-list-python\" aria-label=\"More on How To Remove Items From A List in Python (With Examples)\">Read more<\/a><\/p>\n","protected":false},"author":23,"featured_media":4800,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[141],"tags":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.linuxscrew.com\/wp-json\/wp\/v2\/posts\/4787"}],"collection":[{"href":"https:\/\/www.linuxscrew.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.linuxscrew.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.linuxscrew.com\/wp-json\/wp\/v2\/users\/23"}],"replies":[{"embeddable":true,"href":"https:\/\/www.linuxscrew.com\/wp-json\/wp\/v2\/comments?post=4787"}],"version-history":[{"count":9,"href":"https:\/\/www.linuxscrew.com\/wp-json\/wp\/v2\/posts\/4787\/revisions"}],"predecessor-version":[{"id":10683,"href":"https:\/\/www.linuxscrew.com\/wp-json\/wp\/v2\/posts\/4787\/revisions\/10683"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.linuxscrew.com\/wp-json\/wp\/v2\/media\/4800"}],"wp:attachment":[{"href":"https:\/\/www.linuxscrew.com\/wp-json\/wp\/v2\/media?parent=4787"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.linuxscrew.com\/wp-json\/wp\/v2\/categories?post=4787"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.linuxscrew.com\/wp-json\/wp\/v2\/tags?post=4787"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}