{"id":3398,"date":"2026-11-15T08:31:16","date_gmt":"2026-11-15T08:31:16","guid":{"rendered":"https:\/\/gyanipandit.com\/programming\/?p=3398"},"modified":"2026-11-15T08:31:16","modified_gmt":"2026-11-15T08:31:16","slug":"python-set-remove","status":"publish","type":"post","link":"https:\/\/gyanipandit.com\/programming\/python-set-remove\/","title":{"rendered":"Python Set remove() Method with Examples"},"content":{"rendered":"<p>Hello friends! Adding items to a set is easy, but sooner or later you will also need to take an item out. For that, Python gives us <code>remove()<\/code>. The <code>remove()<\/code> method deletes a specific item from a Python set. If the item is not in the set, it raises a <code>KeyError<\/code>. It changes the set in place and returns <code>None<\/code>. Let&#8217;s understand each of these points with examples.<\/p>\n<pre class=\"wp-block-code\"><code>set.remove(item)<\/code><\/pre>\n<h2>Basic Example<\/h2>\n<pre class=\"wp-block-code\"><code>nums = {10, 20, 30, 40}\nnums.remove(20)\nprint(nums)<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>{40, 10, 30}<\/code><\/pre>\n<p>A set has no fixed order, so the items may be printed in a different order from the one you typed. That is normal.<\/p>\n<p>That was easy. But what happens if you try to remove something that is not in the set at all?<\/p>\n<h2>Removing an Item That Is Not There<\/h2>\n<pre class=\"wp-block-code\"><code>try:\n    nums.remove(99)\nexcept KeyError as e:\n    print(\"KeyError:\", e)<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>KeyError: 99<\/code><\/pre>\n<p>This is the main difference between <code>remove()<\/code> and <a href=\"https:\/\/gyanipandit.com\/programming\/python-set-discard\/\"><code>discard()<\/code><\/a>: <code>discard()<\/code> silently does nothing when the item is missing. Use <code>remove()<\/code> when a missing item would mean a bug that you want to notice.<\/p>\n<p>To avoid the error, check first with the <a href=\"https:\/\/gyanipandit.com\/programming\/membership-operators-in-python\/\"><code>in<\/code> operator<\/a>:<\/p>\n<pre class=\"wp-block-code\"><code>s = {1, 2, 3}\nif 5 in s:\n    s.remove(5)\nprint(s)<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>{1, 2, 3}<\/code><\/pre>\n<h2>remove() Returns None<\/h2>\n<pre class=\"wp-block-code\"><code>print(nums.remove(10))\nprint(nums)<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>None\n{40, 30}<\/code><\/pre>\n<p>Do not write <code>nums = nums.remove(10)<\/code>. That would replace your set with <code>None<\/code>.<\/p>\n<p>Now let&#8217;s use <code>remove()<\/code> in a small real-life example.<\/p>\n<h2>Practical Example: Attendance<\/h2>\n<pre class=\"wp-block-code\"><code>present = {\"Riya\", \"Aman\", \"Kabir\"}\npresent.discard(\"Neha\")    # Neha was never marked present, so no error\npresent.remove(\"Aman\")     # Aman must be in the set\nprint(sorted(present))<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>['Kabir', 'Riya']<\/code><\/pre>\n<h2>Removing Items While Looping<\/h2>\n<p>Friends, this is a mistake almost every beginner makes at least once. You cannot change a set&#8217;s size while looping over it directly:<\/p>\n<pre class=\"wp-block-code\"><code>s = {1, 2, 3, 4, 5, 6}\ntry:\n    for x in s:\n        if x % 2 == 0:\n            s.remove(x)\nexcept RuntimeError as e:\n    print(\"RuntimeError:\", e)<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>RuntimeError: Set changed size during iteration<\/code><\/pre>\n<p>Loop over a copy (<code>for x in list(s):<\/code>) or, better, build a new set with a comprehension:<\/p>\n<pre class=\"wp-block-code\"><code>s = {1, 2, 3, 4, 5, 6}\ns = {x for x in s if x % 2 != 0}\nprint(s)<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>{1, 3, 5}<\/code><\/pre>\n<h2>Removing Several Items at Once<\/h2>\n<pre class=\"wp-block-code\"><code>t = {1, 2, 3, 4, 5}\nt -= {2, 4, 9}\nprint(t)<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>{1, 3, 5}<\/code><\/pre>\n<p>The <code>-=<\/code> operator (or <code>difference_update()<\/code>) removes every item of the other set, and does not complain about items like 9 that were never there.<\/p>\n<h2>Ways to Remove Items from a Set<\/h2>\n<p><code>remove()<\/code> is not the only option. Here is a quick comparison of all the ways to remove items from a set:<\/p>\n<table>\n<thead>\n<tr>\n<th>Method<\/th>\n<th>What it does<\/th>\n<th>Missing item?<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>remove(x)<\/code><\/td>\n<td>Removes x<\/td>\n<td>Raises KeyError<\/td>\n<\/tr>\n<tr>\n<td><code>discard(x)<\/code><\/td>\n<td>Removes x<\/td>\n<td>Does nothing<\/td>\n<\/tr>\n<tr>\n<td><code>pop()<\/code><\/td>\n<td>Removes and returns an arbitrary item<\/td>\n<td>KeyError if the set is empty<\/td>\n<\/tr>\n<tr>\n<td><code>clear()<\/code><\/td>\n<td>Removes everything<\/td>\n<td>Not applicable<\/td>\n<\/tr>\n<tr>\n<td><code>-=<\/code> \/ <code>difference_update()<\/code><\/td>\n<td>Removes many items<\/td>\n<td>Ignored<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>So friends, to sum up: both <code>remove()<\/code> and <a href=\"https:\/\/gyanipandit.com\/programming\/python-set-discard\/\"><code>discard()<\/code><\/a> delete an item, but <code>remove()<\/code> raises a <code>KeyError<\/code> if the item is missing, while <code>discard()<\/code> does nothing. Since sets are unordered and have no indexes, you always remove items by value, not by position. And remember that <code>remove()<\/code> returns <code>None<\/code>, because it changes the set in place. Try these examples yourself and you will never be confused by them again.<\/p>\n<p>More set methods: <a href=\"https:\/\/gyanipandit.com\/programming\/set-methods-in-python\/\">Set methods in Python<\/a> \u00b7 <a href=\"https:\/\/gyanipandit.com\/programming\/python-set-discard\/\">discard()<\/a> \u00b7 <a href=\"https:\/\/gyanipandit.com\/programming\/python-set-pop\/\">pop()<\/a> \u00b7 <a href=\"https:\/\/gyanipandit.com\/programming\/python-set-clear\/\">clear()<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello friends! Adding items to a set is easy, but sooner or later you will also need to take an item out. For that, Python gives us remove(). The remove() method deletes a specific item from a Python set. If the item is not in the set, it raises a KeyError. It changes the set &#8230; <a title=\"Python Set remove() Method with Examples\" class=\"read-more\" href=\"https:\/\/gyanipandit.com\/programming\/python-set-remove\/\" aria-label=\"Read more about Python Set remove() Method with Examples\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[26,27],"class_list":["post-3398","post","type-post","status-publish","format-standard","hentry","category-python-tutorial","tag-python","tag-python-tutorial"],"_links":{"self":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3398","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/comments?post=3398"}],"version-history":[{"count":3,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3398\/revisions"}],"predecessor-version":[{"id":6138,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3398\/revisions\/6138"}],"wp:attachment":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media?parent=3398"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/categories?post=3398"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/tags?post=3398"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}