{"id":29339,"date":"2021-02-22T18:50:09","date_gmt":"2021-02-23T02:50:09","guid":{"rendered":"https:\/\/careerkarma.com\/blog\/?p=29339"},"modified":"2023-12-01T04:08:58","modified_gmt":"2023-12-01T12:08:58","slug":"value-error-list-remove-not-in-list","status":"publish","type":"post","link":"https:\/\/careerkarma.com\/blog\/value-error-list-remove-not-in-list\/","title":{"rendered":"Python ValueError: list.remove(x): x not in list Solution"},"content":{"rendered":"\n<p>You cannot remove an item from a list if it does not appear in said list. The Python error <code>ValueError: list.remove(x): x not in list<\/code> tells you the item you want to remove\u00a0does not appear in the list.<br><\/p>\n\n\n\n<p>In this guide, we&#8217;re going to discuss the cause of the <code>ValueError: list.remove(x): x not in list<\/code>. We will also discuss how to solve this error.\u00a0<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Python ValueError: list.remove(x): x not in list<\/h2>\n\n\n\n<p>The <code>ValueError: list.remove(x): x not in list<\/code> Python error tells us we are using the <em>remove()<\/em> method to remove an item that does not appear in a list. The value <code>x<\/code> will appear in the error message irrespective of the item you are trying to remove. For instance, if you tried to remove <code>123<\/code> from a list, you would still see the same error.<br><\/p>\n\n\n\n<p>Let&#8217;s look at an example of this error in action.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">An Example Scenario<\/h2>\n\n\n\n<p>We&#8217;re building a program that lets a teacher keep track of who has submitted their homework. To do this, we keep a list of all the students in a class who have been given the assignment. If a student hands in their homework, their name is removed from the list.<br><\/p>\n\n\n\n<p>Let&#8217;s start by defining a list of students. Then, we will prompt the user to input a name that should be removed from the list. In the full program, we would save this data to a file. However, to keep things simple, we will not introduce files.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>students = [&quot;Mark&quot;, &quot;Lindsay&quot;, &quot;Peter&quot;]\n\nto_remove = input(&quot;Enter the name of a student who has handed in their homework: &quot;)\n\nstudents.remove(to_remove)\n\nprint(&quot;This student's homework has been recorded.&quot;)<\/pre><\/div>\n\n\n\n<p>Let&#8217;s try to run our program:<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>Enter the name of a student who has handed in their homework: Markk<\/pre><\/div>\n\n\n\n<p>Our program returns:<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>Traceback (most recent call last):\n  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;\nValueError: list.remove(x): x not in list<\/pre><\/div>\n\n\n\n<p>Our program does not work because <code>Markk<\/code> does not appear on our list.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The Solution<\/h2>\n\n\n\n<p>To solve this error, we should first check if the student who we want to remove from our list appears in the list:<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>if to_remove in students:\n\tstudents.remove(to_remove)\n\tprint(&quot;This student's homework has been recorded.&quot;)\nelse:\n\tprint(&quot;This student does not appear in your list.&quot;)<\/pre><\/div>\n\n\n\n<p>Let&#8217;s run our code again:<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>Enter the name of a student who has handed in their homework: Markk\nThis student does not appear in your list.<\/pre><\/div>\n\n\n\n<p>Our code now returns a message rather than a Python error.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>The <code>ValueError: list.remove(x): x not in list<\/code> occurs when you try to remove an item from a list that does not appear in the list. You must use the <em>remove()<\/em> method to remove an item that does not exist in order for this error message to appear.<br><\/p>\n\n\n\n<p>To solve the error, you should first check that the item that you want to remove exists in the list.&nbsp;<br><\/p>\n\n\n\n<p>If you want to learn more about coding in Python, check out our <a href=\"https:\/\/careerkarma.com\/blog\/how-to-learn-python\/\">How to Learn Python guide<\/a>. You will find top tips on learning Python. The guide also contains some learning resources you can use to build your understanding of the Python programming language.<\/p>\n","protected":false},"excerpt":{"rendered":"You cannot remove an item from a list if it does not appear in said list. The Python error ValueError: list.remove(x): x not in list tells you the item you want to remove\u00a0does not appear in the list. In this guide, we're going to discuss the cause of the ValueError: list.remove(x): x not in list.&hellip;","protected":false},"author":240,"featured_media":2789,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[16578],"tags":[],"class_list":{"0":"post-29339","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-python"},"acf":{"post_sub_title":"","sprint_id":"","query_class":"Python","school_sft":"","parent_sft":"","school_privacy_policy":"","has_review":null,"is_sponser_post":"","is_guest_post":""},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.0 (Yoast SEO v27.0) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>ValueError: list.remove(x): x not in list<\/title>\n<meta name=\"description\" content=\"Learn the causes of and the solution to the ValueError: list.remove(x): x not in list Python error on Career Karma.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/careerkarma.com\/blog\/value-error-list-remove-not-in-list\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python ValueError: list.remove(x): x not in list Solution\" \/>\n<meta property=\"og:description\" content=\"Learn the causes of and the solution to the ValueError: list.remove(x): x not in list Python error on Career Karma.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/careerkarma.com\/blog\/value-error-list-remove-not-in-list\/\" \/>\n<meta property=\"og:site_name\" content=\"Career Karma\" \/>\n<meta property=\"article:publisher\" content=\"http:\/\/facebook.com\/careerkarmaapp\" \/>\n<meta property=\"article:published_time\" content=\"2021-02-23T02:50:09+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-01T12:08:58+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2019\/06\/code-coding-connection-943096.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"800\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"James Gallagher\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@career_karma\" \/>\n<meta name=\"twitter:site\" content=\"@career_karma\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"James Gallagher\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/careerkarma.com\/blog\/value-error-list-remove-not-in-list\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/value-error-list-remove-not-in-list\/\"},\"author\":{\"name\":\"James Gallagher\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94\"},\"headline\":\"Python ValueError: list.remove(x): x not in list Solution\",\"datePublished\":\"2021-02-23T02:50:09+00:00\",\"dateModified\":\"2023-12-01T12:08:58+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/value-error-list-remove-not-in-list\/\"},\"wordCount\":414,\"commentCount\":0,\"image\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/value-error-list-remove-not-in-list\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2019\/06\/code-coding-connection-943096.jpg\",\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/careerkarma.com\/blog\/value-error-list-remove-not-in-list\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/careerkarma.com\/blog\/value-error-list-remove-not-in-list\/\",\"url\":\"https:\/\/careerkarma.com\/blog\/value-error-list-remove-not-in-list\/\",\"name\":\"ValueError: list.remove(x): x not in list\",\"isPartOf\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/value-error-list-remove-not-in-list\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/value-error-list-remove-not-in-list\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2019\/06\/code-coding-connection-943096.jpg\",\"datePublished\":\"2021-02-23T02:50:09+00:00\",\"dateModified\":\"2023-12-01T12:08:58+00:00\",\"author\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94\"},\"description\":\"Learn the causes of and the solution to the ValueError: list.remove(x): x not in list Python error on Career Karma.\",\"breadcrumb\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/value-error-list-remove-not-in-list\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/careerkarma.com\/blog\/value-error-list-remove-not-in-list\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/careerkarma.com\/blog\/value-error-list-remove-not-in-list\/#primaryimage\",\"url\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2019\/06\/code-coding-connection-943096.jpg\",\"contentUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2019\/06\/code-coding-connection-943096.jpg\",\"width\":1200,\"height\":800},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/careerkarma.com\/blog\/value-error-list-remove-not-in-list\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog\",\"item\":\"https:\/\/careerkarma.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python\",\"item\":\"https:\/\/careerkarma.com\/blog\/python\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Python ValueError: list.remove(x): x not in list Solution\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#website\",\"url\":\"https:\/\/careerkarma.com\/blog\/\",\"name\":\"Career Karma\",\"description\":\"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/careerkarma.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94\",\"name\":\"James Gallagher\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg\",\"contentUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg\",\"caption\":\"James Gallagher\"},\"description\":\"James Gallagher is a self-taught programmer and the technical content manager at Career Karma. He has experience in range of programming languages and extensive expertise in Python, HTML, CSS, and JavaScript. James has written hundreds of programming tutorials, and he frequently contributes to publications like Codecademy, Treehouse, Repl.it, Afrotech, and others.\",\"url\":\"https:\/\/careerkarma.com\/blog\/author\/jamesgallagher\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"ValueError: list.remove(x): x not in list","description":"Learn the causes of and the solution to the ValueError: list.remove(x): x not in list Python error on Career Karma.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/careerkarma.com\/blog\/value-error-list-remove-not-in-list\/","og_locale":"en_US","og_type":"article","og_title":"Python ValueError: list.remove(x): x not in list Solution","og_description":"Learn the causes of and the solution to the ValueError: list.remove(x): x not in list Python error on Career Karma.","og_url":"https:\/\/careerkarma.com\/blog\/value-error-list-remove-not-in-list\/","og_site_name":"Career Karma","article_publisher":"http:\/\/facebook.com\/careerkarmaapp","article_published_time":"2021-02-23T02:50:09+00:00","article_modified_time":"2023-12-01T12:08:58+00:00","og_image":[{"width":1200,"height":800,"url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2019\/06\/code-coding-connection-943096.jpg","type":"image\/jpeg"}],"author":"James Gallagher","twitter_card":"summary_large_image","twitter_creator":"@career_karma","twitter_site":"@career_karma","twitter_misc":{"Written by":"James Gallagher","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/careerkarma.com\/blog\/value-error-list-remove-not-in-list\/#article","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/value-error-list-remove-not-in-list\/"},"author":{"name":"James Gallagher","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94"},"headline":"Python ValueError: list.remove(x): x not in list Solution","datePublished":"2021-02-23T02:50:09+00:00","dateModified":"2023-12-01T12:08:58+00:00","mainEntityOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/value-error-list-remove-not-in-list\/"},"wordCount":414,"commentCount":0,"image":{"@id":"https:\/\/careerkarma.com\/blog\/value-error-list-remove-not-in-list\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2019\/06\/code-coding-connection-943096.jpg","articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/careerkarma.com\/blog\/value-error-list-remove-not-in-list\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/careerkarma.com\/blog\/value-error-list-remove-not-in-list\/","url":"https:\/\/careerkarma.com\/blog\/value-error-list-remove-not-in-list\/","name":"ValueError: list.remove(x): x not in list","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/value-error-list-remove-not-in-list\/#primaryimage"},"image":{"@id":"https:\/\/careerkarma.com\/blog\/value-error-list-remove-not-in-list\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2019\/06\/code-coding-connection-943096.jpg","datePublished":"2021-02-23T02:50:09+00:00","dateModified":"2023-12-01T12:08:58+00:00","author":{"@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94"},"description":"Learn the causes of and the solution to the ValueError: list.remove(x): x not in list Python error on Career Karma.","breadcrumb":{"@id":"https:\/\/careerkarma.com\/blog\/value-error-list-remove-not-in-list\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/careerkarma.com\/blog\/value-error-list-remove-not-in-list\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/value-error-list-remove-not-in-list\/#primaryimage","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2019\/06\/code-coding-connection-943096.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2019\/06\/code-coding-connection-943096.jpg","width":1200,"height":800},{"@type":"BreadcrumbList","@id":"https:\/\/careerkarma.com\/blog\/value-error-list-remove-not-in-list\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/careerkarma.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Python","item":"https:\/\/careerkarma.com\/blog\/python\/"},{"@type":"ListItem","position":3,"name":"Python ValueError: list.remove(x): x not in list Solution"}]},{"@type":"WebSite","@id":"https:\/\/careerkarma.com\/blog\/#website","url":"https:\/\/careerkarma.com\/blog\/","name":"Career Karma","description":"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/careerkarma.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94","name":"James Gallagher","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg","caption":"James Gallagher"},"description":"James Gallagher is a self-taught programmer and the technical content manager at Career Karma. He has experience in range of programming languages and extensive expertise in Python, HTML, CSS, and JavaScript. James has written hundreds of programming tutorials, and he frequently contributes to publications like Codecademy, Treehouse, Repl.it, Afrotech, and others.","url":"https:\/\/careerkarma.com\/blog\/author\/jamesgallagher\/"}]}},"_links":{"self":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/29339","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/users\/240"}],"replies":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/comments?post=29339"}],"version-history":[{"count":0,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/29339\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media\/2789"}],"wp:attachment":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media?parent=29339"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/categories?post=29339"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/tags?post=29339"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}