{"id":129229,"date":"2022-10-26T12:36:52","date_gmt":"2022-10-26T12:36:52","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=830190"},"modified":"2022-10-26T12:36:52","modified_gmt":"2022-10-26T12:36:52","slug":"6-ways-to-remove-python-list-elements","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2022\/10\/26\/6-ways-to-remove-python-list-elements\/","title":{"rendered":"6 Ways to Remove Python List Elements"},"content":{"rendered":"\n<div class=\"kk-star-ratings kksr-auto kksr-align-left kksr-valign-top\" data-payload=\"{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;830190&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;2&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;5&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;5\\\/5 - (2 votes)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;width&quot;:&quot;142.5&quot;,&quot;_legend&quot;:&quot;{score}\\\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}\">\n<div class=\"kksr-stars\">\n<div class=\"kksr-stars-inactive\">\n<div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div class=\"kksr-stars-active\" style=\"width: 142.5px;\">\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/div>\n<div class=\"kksr-legend\" style=\"font-size: 19.2px;\"> 5\/5 &#8211; (2 votes) <\/div>\n<\/div>\n<h2 class=\"wp-embed-aspect-16-9 wp-has-aspect-ratio\">Problem Formulation and Solution Overview<\/h2>\n<p class=\"wp-embed-aspect-16-9 wp-has-aspect-ratio\">This article will show you how 6 ways to remove <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-lists\/\" target=\"_blank\"><code>List<\/code><\/a> elements in Python.<\/p>\n<p>To make it more interesting, we have the following running scenario:<\/p>\n<p class=\"wp-embed-aspect-16-9 wp-has-aspect-ratio\"><em>Suppose you have a Christmas <em><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-lists\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-lists\/\" target=\"_blank\"><code>List<\/code><\/a><\/em><\/em> <em>containing everyone to buy a gift for. Once a gift is purchased, remove this person from the <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-lists\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-lists\/\" target=\"_blank\"><code>List<\/code><\/a>. Once all gifts have been purchased, remove the entire <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-lists\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-lists\/\" target=\"_blank\"><code>List<\/code><\/a>.<\/em><\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/10\/xmas_lst_02.png\" alt=\"\" class=\"wp-image-837170\" width=\"609\" height=\"159\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/10\/xmas_lst_02.png 900w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/10\/xmas_lst_02-300x79.png 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/10\/xmas_lst_02-768x201.png 768w\" sizes=\"auto, (max-width: 609px) 100vw, 609px\" \/><\/figure>\n<\/div>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<tbody>\n<tr>\n<td><code>xmas_list = ['Anna', 'Elin', 'Inger', 'Asa', 'Sofie', 'Gunnel', 'Linn']<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<hr class=\"wp-block-separator has-alpha-channel-opacity wp-embed-aspect-16-9 wp-has-aspect-ratio\" \/>\n<p class=\"wp-embed-aspect-16-9 wp-has-aspect-ratio has-global-color-8-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f4ac.png\" alt=\"\ud83d\udcac\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> <strong>Question<\/strong>: How would we write code to remove items from a Python <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-lists\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-lists\/\" target=\"_blank\">List<\/a><em>?<\/em><\/p>\n<p class=\"wp-embed-aspect-16-9 wp-has-aspect-ratio\">We can accomplish this task by one of the following options:<\/p>\n<ul type=\"video\" class=\"wp-embed-aspect-16-9 wp-has-aspect-ratio\">\n<li><strong>Method <\/strong>1: Use the <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-cheat-sheet\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-cheat-sheet\/\" target=\"_blank\"><code>del<\/code><\/a> Keyword<\/li>\n<li><strong>Method <\/strong>2: Use <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-set-remove\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-set-remove\/\" target=\"_blank\"><code>remove()<\/code><\/a> and a <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-loops\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-loops\/\" target=\"_blank\"><code>for<\/code><\/a> Loop<\/li>\n<li><strong>Method <\/strong>3: Use <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/introduction-to-slicing-in-python\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/introduction-to-slicing-in-python\/\" target=\"_blank\">slicing<\/a><\/li>\n<li><strong>Method 4<\/strong>: Use <a href=\"https:\/\/blog.finxter.com\/python-list-pop\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-list-pop\/\"><code>pop()<\/code><\/a><\/li>\n<li><strong>Method 5<\/strong>: Use <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/list-comprehension\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/list-comprehension\/\" target=\"_blank\">List Comprehension<\/a><\/li>\n<li><strong>Method 6<\/strong>: Use <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-list-clear\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-list-clear\/\" target=\"_blank\"><code>clear()<\/code><\/a><\/li>\n<\/ul>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n<h2>Method 1: Use the del Keyword<\/h2>\n<p class=\"has-global-color-8-background-color has-background\">This method uses Python&#8217;s <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-cheat-sheet\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-cheat-sheet\/\" target=\"_blank\"><code>del<\/code><\/a> Keyword and highlights its ability to remove one <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-lists\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-lists\/\" target=\"_blank\">List<\/a> element and all <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-lists\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-lists\/\" target=\"_blank\">List<\/a> elements. <\/p>\n<p><span style=\"text-decoration: underline\">Remove One List Element<\/span><\/p>\n<p>In this scenario, <code>Asa's<\/code> gift has been purchased and will be removed from the <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-lists\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-lists\/\" target=\"_blank\"><code>List<\/code><\/a>. <\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"2\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">xmas_list = ['Anna', 'Elin', 'Inger', 'Asa', 'Sofie', 'Gunnel', 'Linn']\ndel xmas_list[3]\nprint(xmas_list)<\/pre>\n<p>As shown on the highlighted line, <code>Asa<\/code> is removed from the <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-lists\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-lists\/\" target=\"_blank\">List<\/a> by using <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-cheat-sheet\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-cheat-sheet\/\" target=\"_blank\"><code>del<\/code><\/a>, referencing <code>xmas_list<\/code> and specifying Asa&#8217;s location (<code>[3]<\/code>).<\/p>\n<p>When <code>xmas_list<\/code> is output to the terminal, the following displays.<\/p>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<tbody>\n<tr>\n<td><code>['Anna', 'Elin', 'Inger', 'Sofie', 'Gunnel', 'Linn']<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<p><span style=\"text-decoration: underline\">Remove All List Elements<\/span><\/p>\n<p>In this scenario, all gifts have been purchased, and all <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-lists\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-lists\/\" target=\"_blank\">List<\/a> elements will be removed.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"2\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">xmas_list = ['Anna', 'Elin', 'Inger', 'Asa', 'Sofie', 'Gunnel', 'Linn']\ndel xmas_list\nprint(xmas_list)<\/pre>\n<p>As shown on the highlighted line, all elements of <code>xmas_list<\/code> are removed by using <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-cheat-sheet\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-cheat-sheet\/\" target=\"_blank\"><code>del<\/code><\/a> and referencing <code>xmas_list<\/code>. <\/p>\n<p>When <code>xmas_list<\/code> is output to the terminal, the following error is generated.<\/p>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<tbody>\n<tr>\n<td><code>NameError: name 'xmas_list' is not defined<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<p class=\"has-global-color-8-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f4a1.png\" alt=\"\ud83d\udca1\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><strong>Note<\/strong>: This error is generated because the variable <code>xmas_list<\/code> no longer exists in memory.<\/p>\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube\"><a href=\"https:\/\/blog.finxter.com\/6-ways-to-remove-python-list-elements\/\"><img decoding=\"async\" src=\"https:\/\/blog.finxter.com\/wp-content\/plugins\/wp-youtube-lyte\/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2FqXOFngx0bQU%2Fhqdefault.jpg\" alt=\"YouTube Video\"><\/a><figcaption><\/figcaption><\/figure>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n<h2>Method 2: Use remove() and a For Loop<\/h2>\n<p class=\"has-global-color-8-background-color has-background\">This example uses the <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-set-remove\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-set-remove\/\" target=\"_blank\"><code>remove()<\/code><\/a> function in conjunction with a <a href=\"https:\/\/blog.finxter.com\/python-loops\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-loops\/\"><code>for<\/code><\/a> loop to remove one <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-lists\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-lists\/\" target=\"_blank\">List<\/a> element and all <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-lists\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-lists\/\" target=\"_blank\">List<\/a> elements. <\/p>\n<p><span style=\"text-decoration: underline\">Remove One List Element<\/span><\/p>\n<p>In this scenario, <code><code>Elin's<\/code><\/code> gift has been purchased and will be removed from the <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-lists\/\" target=\"_blank\">List<\/a>.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"2\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">xmas_list = ['Anna', 'Elin', 'Inger', 'Asa', 'Sofie', 'Gunnel', 'Linn']\nxmas_list.remove('Elin')\nprint(xmas_list)<\/pre>\n<p>As shown on the highlighted line, <code>Elin<\/code> is removed from the <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-lists\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-lists\/\" target=\"_blank\">List<\/a> using the <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-set-remove\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-set-remove\/\" target=\"_blank\"><code>remove()<\/code><\/a> function and passing Elin&#8217;s name as an argument. <\/p>\n<p>When <code>xmas_list<\/code> is output to the terminal, the following displays.<\/p>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<tbody>\n<tr>\n<td><code>['Anna', 'Inger', 'Asa', 'Sofie', 'Gunnel', 'Linn']<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<p><span style=\"text-decoration: underline\">Remove All List Elements<\/span><\/p>\n<p>In this scenario, all gifts have been purchased, and all <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-lists\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-lists\/\" target=\"_blank\"><code>List<\/code><\/a> elements will be removed.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"2-3\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">xmas_list = ['Anna', 'Elin', 'Inger', 'Asa', 'Sofie', 'Gunnel', 'Linn']\nfor item in xmas_list.copy(): xmas_list.remove(item)\nprint(xmas_list)<\/pre>\n<p>As shown on the first highlighted line, a <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-loops\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-loops\/\" target=\"_blank\"><code>for<\/code><\/a> loop is instantiated. This loop declares a shallow copy of the <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-lists\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-lists\/\" target=\"_blank\">List<\/a> to iterate. <\/p>\n<p>On each iteration, the <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-set-remove\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-set-remove\/\" target=\"_blank\"><code>remove()<\/code><\/a> function is called and passed the current name in <code>xmas_list<\/code> as an argument (see below) and removed. <\/p>\n<p>For example:<\/p>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<tbody>\n<tr>\n<td><code>Anna<br \/>Elin<br \/>Inger<br \/>Asa<br \/>Sofie<br \/>Gunnel<br \/>Linn<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<p>When <code>xmas_list<\/code> is output to the terminal, an empty <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-lists\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-lists\/\" target=\"_blank\"><code>List<\/code><\/a> displays.<\/p>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<tbody>\n<tr>\n<td><code>[]<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<p class=\"has-global-color-8-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f4a1.png\" alt=\"\ud83d\udca1\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><strong>Note<\/strong>: A shallow copy creates a reference to the original <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-lists\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-lists\/\" target=\"_blank\">List<\/a>. For further details, view the video below.<\/p>\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube\"><a href=\"https:\/\/blog.finxter.com\/6-ways-to-remove-python-list-elements\/\"><img decoding=\"async\" src=\"https:\/\/blog.finxter.com\/wp-content\/plugins\/wp-youtube-lyte\/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2FVhLrz7ivZbc%2Fhqdefault.jpg\" alt=\"YouTube Video\"><\/a><figcaption><\/figcaption><\/figure>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n<h2>Method 3: Use slicing<\/h2>\n<p class=\"has-global-color-8-background-color has-background\">This method uses <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/introduction-to-slicing-in-python\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/introduction-to-slicing-in-python\/\" target=\"_blank\">slicing<\/a> to remove one <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-lists\/\" target=\"_blank\">List<\/a> element and all <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-lists\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-lists\/\" target=\"_blank\">List<\/a> elements. <\/p>\n<p><span style=\"text-decoration: underline\">Remove One List Element<\/span><\/p>\n<p>In this scenario, <code><code>Anna<code>'s<\/code><\/code><\/code> gift has been purchased and will be removed from the <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-lists\/\" target=\"_blank\">List<\/a>.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"2\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">xmas_list = ['Anna', 'Elin', 'Inger', 'Asa', 'Sofie', 'Gunnel', 'Linn']\nxmas_list = xmas_list[1:]\nprint(xmas_list)<\/pre>\n<p>As shown on the highlighted line, <code>Anna<\/code> is removed from the <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-lists\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-lists\/\" target=\"_blank\"><code>List<\/code><\/a> using <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/introduction-to-slicing-in-python\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/introduction-to-slicing-in-python\/\" target=\"_blank\"><code>slicing<\/code><\/a>. <\/p>\n<p>When <code>xmas_list<\/code> is output to the terminal, the following displays.<\/p>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<tbody>\n<tr>\n<td><code>['Elin', 'Inger', 'Asa', 'Sofie', 'Gunnel', 'Linn']<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<p><span style=\"text-decoration: underline\">Remove All List Elements<\/span><\/p>\n<p>In this scenario, all gifts have been purchased, and all <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-lists\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-lists\/\" target=\"_blank\"><code>List<\/code><\/a> elements will be removed.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"2\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">xmas_list = ['Anna', 'Elin', 'Inger', 'Asa', 'Sofie', 'Gunnel', 'Linn']\nxmas_list = []\nprint(xmas_list)<\/pre>\n<p>As shown on the highlighted line, all <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-lists\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-lists\/\" target=\"_blank\"><code>List<\/code><\/a> elements are removed by declaring an empty <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-lists\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-lists\/\" target=\"_blank\"><code>List<\/code><\/a>. <\/p>\n<p>When <code>xmas_list<\/code> is output to the terminal, an empty <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-lists\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-lists\/\" target=\"_blank\"><code>List<\/code><\/a> displays.<\/p>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<tbody>\n<tr>\n<td><code>[]<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube\"><a href=\"https:\/\/blog.finxter.com\/6-ways-to-remove-python-list-elements\/\"><img decoding=\"async\" src=\"https:\/\/blog.finxter.com\/wp-content\/plugins\/wp-youtube-lyte\/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2FD2ZueuWXST8%2Fhqdefault.jpg\" alt=\"YouTube Video\"><\/a><figcaption><\/figcaption><\/figure>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n<h2>Method 4: Use pop()<\/h2>\n<p class=\"has-global-color-8-background-color has-background\">This method uses the <a href=\"https:\/\/blog.finxter.com\/python-list-pop\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-list-pop\/\"><code>pop()<\/code><\/a> function to remove one <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-lists\/\" target=\"_blank\">List<\/a> element and all <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-lists\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-lists\/\" target=\"_blank\">List<\/a> elements. <\/p>\n<p><span style=\"text-decoration: underline\">Remove One List Element<\/span><\/p>\n<p>In this scenario, <code><code>Linn'<code>s<\/code><\/code><\/code> gift has been purchased and will be removed from the <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-lists\/\" target=\"_blank\"><code>List<\/code><\/a>.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"2-3\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">xmas_list = ['Anna', 'Elin', 'Inger', 'Asa', 'Sofie', 'Gunnel', 'Linn']\nxmas_list.pop()\nxmas_list.pop(2)\nprint(xmas_list)<\/pre>\n<p>As shown on the first highlighted line, the <a href=\"https:\/\/blog.finxter.com\/python-list-pop\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-list-pop\/\"><code>pop()<\/code><\/a> method is appended to the <code>xmas_list<\/code>. This lets Python know to remove a List element from said <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-lists\/\" target=\"_blank\">List<\/a>. Since no element is specified, the last element is removed (<code>Linn<\/code>).<\/p>\n<p>On the second highlighted line, the <a href=\"https:\/\/blog.finxter.com\/python-list-pop\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-list-pop\/\"><code>pop()<\/code><\/a> method is appended to the <code>xmas_list<\/code> and passed one (1) argument: the element to remove (<code>2<\/code>). This action removes Inger.<\/p>\n<p>When <code>xmas_list<\/code> is output to the terminal, the following displays.<\/p>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<tbody>\n<tr>\n<td><code>['Anna', 'Elin', 'Asa', 'Sofie', 'Gunnel']<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<p class=\"has-global-color-8-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f4a1.png\" alt=\"\ud83d\udca1\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><strong>Note<\/strong>: Both Linn and Inger are no longer in <code>xmas_list<\/code>.<\/p>\n<p><span style=\"text-decoration: underline\">Remove All List Elements<\/span><\/p>\n<p>In this scenario, all gifts have been purchased, and all <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-lists\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-lists\/\" target=\"_blank\"><code>List<\/code><\/a> elements will be removed.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"3-4\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">xmas_list = ['Anna', 'Elin', 'Inger', 'Asa', 'Sofie', 'Gunnel', 'Linn'] for i in xmas_list.copy(): xmas_list.pop()\nprint(xmas_list)<\/pre>\n<p>As shown on the first highlighted line, a <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-loops\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-loops\/\" target=\"_blank\"><code>for<\/code><\/a> loop is instantiated. This loop declares a shallow copy of the <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-lists\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-lists\/\" target=\"_blank\">List<\/a> to iterate. <\/p>\n<p>On each iteration, the <a href=\"https:\/\/blog.finxter.com\/python-list-pop\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-list-pop\/\"><code>pop()<\/code><\/a> method is called. Since no argument is passed, the last element is removed.<\/p>\n<p>When <code>xmas_list<\/code> is output to the terminal, an empty <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-lists\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-lists\/\" target=\"_blank\">List<\/a> displays.<\/p>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<tbody>\n<tr>\n<td><code>[]<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube\"><a href=\"https:\/\/blog.finxter.com\/6-ways-to-remove-python-list-elements\/\"><img decoding=\"async\" src=\"https:\/\/blog.finxter.com\/wp-content\/plugins\/wp-youtube-lyte\/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2Fr9VhOWN5oEg%2Fhqdefault.jpg\" alt=\"YouTube Video\"><\/a><figcaption><\/figcaption><\/figure>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n<h2>Method 5: Use List Comprehension<\/h2>\n<p class=\"has-global-color-8-background-color has-background\">This method uses <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/list-comprehension\/\" target=\"_blank\">List Comprehension<\/a> to remove all <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-lists\/\" target=\"_blank\">List<\/a> elements that do not meet the specified criteria.<\/p>\n<p><span style=\"text-decoration: underline\">Remove One List Element<\/span><\/p>\n<p>In this scenario, <code><code>Gunnel's <\/code><\/code>gift has been purchased and will be removed from the <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-lists\/\" target=\"_blank\">List<\/a>.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"2\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">xmas_list = ['Anna', 'Elin', 'Inger', 'Asa', 'Sofie', 'Gunnel', 'Linn']\nxmas_list = [value for value in xmas_list if value != 'Gunnel']\nprint(xmas_list)<\/pre>\n<p>When <code>xmas_list<\/code> is output to the terminal, the following displays.<\/p>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<tbody>\n<tr>\n<td><code>['Anna', 'Elin', 'Inger', 'Asa', 'Sofie', 'Linn']<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<p class=\"has-global-color-8-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f4a1.png\" alt=\"\ud83d\udca1\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><strong>Note<\/strong>: To remove all List elements, pass it empty brackets as shown follows: (<code>xmas_list = []<\/code>).<\/p>\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube\"><a href=\"https:\/\/blog.finxter.com\/6-ways-to-remove-python-list-elements\/\"><img decoding=\"async\" src=\"https:\/\/blog.finxter.com\/wp-content\/plugins\/wp-youtube-lyte\/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2FaBC0VhpXkOQ%2Fhqdefault.jpg\" alt=\"YouTube Video\"><\/a><figcaption><\/figcaption><\/figure>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n<h2>Method 6: Use clear()<\/h2>\n<p class=\"has-global-color-8-background-color has-background\">This method uses <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-list-clear\/\" target=\"_blank\"><code>clear()<\/code><\/a> to remove all <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-lists\/\" target=\"_blank\">List<\/a> elements.<\/p>\n<p>In this scenario, all gifts have been purchased, and all <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-lists\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-lists\/\" target=\"_blank\">List<\/a> elements will be removed.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"2\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">xmas_list = ['Anna', 'Elin', 'Inger', 'Asa', 'Sofie', 'Gunnel', 'Linn']\nxmas_list.clear()\nprint(xmas_list)<\/pre>\n<p>As shown on the highlighted line, all <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-lists\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-lists\/\" target=\"_blank\">List<\/a> elements are removed by appending the <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-list-clear\/\" target=\"_blank\"><code>clear()<\/code><\/a> function to <code>xmas_list<\/code>.<\/p>\n<p>When <code>xmas_list<\/code> is output to the terminal, an empty <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-lists\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-lists\/\" target=\"_blank\">List<\/a> displays.<\/p>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<tbody>\n<tr>\n<td><code>[]<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube\"><a href=\"https:\/\/blog.finxter.com\/6-ways-to-remove-python-list-elements\/\"><img decoding=\"async\" src=\"https:\/\/blog.finxter.com\/wp-content\/plugins\/wp-youtube-lyte\/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2FfqscJGzsDKc%2Fhqdefault.jpg\" alt=\"YouTube Video\"><\/a><figcaption><\/figcaption><\/figure>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n<h2>Summary<\/h2>\n<p>This article has provided six (6) ways to remove <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-lists\/\" target=\"_blank\">List<\/a> elements to select the best fit for your coding requirements.<\/p>\n<p>Good Luck &amp; Happy Coding!<\/p>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n<h2>Programming Humor<\/h2>\n<p class=\"has-global-color-8-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f4a1.png\" alt=\"\ud83d\udca1\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> Programming is 10% science, 20% ingenuity, and 70% getting the ingenuity to work with the science.<\/p>\n<p>~~~<\/p>\n<ul class=\"has-global-color-8-background-color has-background\">\n<li><strong>Question<\/strong>: Why do Java programmers wear glasses?<\/li>\n<li><strong>Answer<\/strong>: Because they cannot C# &#8230;!<\/li>\n<\/ul>\n<p>Feel free to check out <a href=\"https:\/\/blog.finxter.com\/what-is-the-geekiest-joke\/\" data-type=\"post\" data-id=\"617\" target=\"_blank\" rel=\"noreferrer noopener\">our blog article<\/a> with more coding jokes. <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f609.png\" alt=\"\ud83d\ude09\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>5\/5 &#8211; (2 votes) Problem Formulation and Solution Overview This article will show you how 6 ways to remove List elements in Python. To make it more interesting, we have the following running scenario: Suppose you have a Christmas List containing everyone to buy a gift for. Once a gift is purchased, remove this person [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-129229","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/129229","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=129229"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/129229\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=129229"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=129229"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=129229"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}