{"id":3409,"date":"2026-11-15T11:47:10","date_gmt":"2026-11-15T11:47:10","guid":{"rendered":"https:\/\/gyanipandit.com\/programming\/?p=3409"},"modified":"2026-11-15T11:47:10","modified_gmt":"2026-11-15T11:47:10","slug":"python-set-discard","status":"publish","type":"post","link":"https:\/\/gyanipandit.com\/programming\/python-set-discard\/","title":{"rendered":"Python Set discard() Method with Examples"},"content":{"rendered":"<p>Hello friends! Sometimes you want to remove an item from a set, but you are not sure whether it is even there. In such cases, <code>discard()<\/code> is your best friend. The <code>discard()<\/code> method removes an item from a Python set <strong>if it is present<\/strong>. If the item is not in the set, it simply does nothing: no error, no warning. It changes the set in place and returns <code>None<\/code>. Let&#8217;s see it in action.<\/p>\n<pre class=\"wp-block-code\"><code>set.discard(item)<\/code><\/pre>\n<h2>Basic Example<\/h2>\n<pre class=\"wp-block-code\"><code>nums = {10, 20, 30, 40}\nnums.discard(20)\nprint(nums)\n\nnums.discard(99)     # 99 is not in the set: nothing happens\nprint(nums)<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>{40, 10, 30}\n{40, 10, 30}<\/code><\/pre>\n<h2>discard() vs remove()<\/h2>\n<p>Python also has <code>remove()<\/code>, which looks very similar. So what is the difference? Let&#8217;s compare them side by side:<\/p>\n<table>\n<thead>\n<tr>\n<th><\/th>\n<th><code>discard(x)<\/code><\/th>\n<th><a href=\"https:\/\/gyanipandit.com\/programming\/python-set-remove\/\"><code>remove(x)<\/code><\/a><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>x is in the set<\/td>\n<td>Removes it<\/td>\n<td>Removes it<\/td>\n<\/tr>\n<tr>\n<td>x is not in the set<\/td>\n<td>Does nothing<\/td>\n<td>Raises <code>KeyError<\/code><\/td>\n<\/tr>\n<tr>\n<td>Returns<\/td>\n<td><code>None<\/code><\/td>\n<td><code>None<\/code><\/td>\n<\/tr>\n<tr>\n<td>Use when<\/td>\n<td>The item may or may not be there<\/td>\n<td>The item must be there, and missing means a bug<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>So <code>discard()<\/code> saves you from writing <code>if x in s: s.remove(x)<\/code>.<\/p>\n<h2>discard() Returns None<\/h2>\n<pre class=\"wp-block-code\"><code>print(nums.discard(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>Now let&#8217;s use <code>discard()<\/code> in a practical example.<\/p>\n<h2>Practical Example: Removing Even Numbers Safely<\/h2>\n<p>You cannot change a set while looping over it, so loop over a copy:<\/p>\n<pre class=\"wp-block-code\"><code>s = {1, 2, 3, 4, 5, 6}\nfor x in list(s):\n    if x % 2 == 0:\n        s.discard(x)\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<\/h2>\n<pre class=\"wp-block-code\"><code>t = {1, 2, 3, 4, 5}\nt -= {2, 4, 9}\nt.difference_update([1])\nprint(t)<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>{3, 5}<\/code><\/pre>\n<p><code>discard()<\/code> takes only one item. To remove many at once, use <code>-=<\/code> or <code>difference_update()<\/code>, which also ignore missing items.<\/p>\n<p>Before we finish, there is one special case you should know about.<\/p>\n<h2>A Note on frozenset<\/h2>\n<pre class=\"wp-block-code\"><code>f = frozenset({1, 2})\ntry:\n    f.discard(1)\nexcept AttributeError as e:\n    print(\"AttributeError:\", e)<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>AttributeError: 'frozenset' object has no attribute 'discard'<\/code><\/pre>\n<p>A <code>frozenset<\/code> cannot be changed, so it has no <code>discard()<\/code>, <code>remove()<\/code> or <code>add()<\/code>.<\/p>\n<p>So friends, remember: <code>discard()<\/code> never raises an error when the item is not found, and that is exactly why it exists. If you want an error for a missing item, use <code>remove()<\/code> instead. Both are equally fast, so choose based on whether a missing item should be treated as a problem. And since <code>discard()<\/code> removes only one item per call, use <code>-=<\/code> or <code>difference_update()<\/code> when you need to remove several. Practise with your own sets and it will soon feel natural.<\/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-remove\/\">remove()<\/a> \u00b7 <a href=\"https:\/\/gyanipandit.com\/programming\/python-set-difference-update\/\">difference_update()<\/a> \u00b7 <a href=\"https:\/\/gyanipandit.com\/programming\/python-set-add\/\">add()<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello friends! Sometimes you want to remove an item from a set, but you are not sure whether it is even there. In such cases, discard() is your best friend. The discard() method removes an item from a Python set if it is present. If the item is not in the set, it simply does &#8230; <a title=\"Python Set discard() Method with Examples\" class=\"read-more\" href=\"https:\/\/gyanipandit.com\/programming\/python-set-discard\/\" aria-label=\"Read more about Python Set discard() 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-3409","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\/3409","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=3409"}],"version-history":[{"count":3,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3409\/revisions"}],"predecessor-version":[{"id":6139,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3409\/revisions\/6139"}],"wp:attachment":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media?parent=3409"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/categories?post=3409"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/tags?post=3409"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}