{"id":3405,"date":"2026-11-16T06:07:38","date_gmt":"2026-11-16T06:07:38","guid":{"rendered":"https:\/\/gyanipandit.com\/programming\/?p=3405"},"modified":"2026-11-16T06:07:38","modified_gmt":"2026-11-16T06:07:38","slug":"python-set-issubset","status":"publish","type":"post","link":"https:\/\/gyanipandit.com\/programming\/python-set-issubset\/","title":{"rendered":"Python Set issubset() Method with Examples"},"content":{"rendered":"<p>Hello friends! Remember subsets from your school mathematics? Python lets you check them in one line. The <code>issubset()<\/code> method checks whether <strong>every<\/strong> item of one set is also present in another set. If yes, the first set is a <em>subset<\/em> of the second, and the method returns <code>True<\/code>; otherwise it returns <code>False<\/code>. Let&#8217;s understand it with examples.<\/p>\n<pre class=\"wp-block-code\"><code>a.issubset(b)     # is every item of a also in b?\na &lt;= b            # same check, operator form<\/code><\/pre>\n<h2>Basic Example<\/h2>\n<pre class=\"wp-block-code\"><code>a = {1, 2}\nb = {1, 2, 3, 4}\nprint(a.issubset(b))\nprint(b.issubset(a))\nprint(a &lt;= b)<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>True\nFalse\nTrue<\/code><\/pre>\n<p>1 and 2 are both in <code>b<\/code>, so <code>a<\/code> is a subset of <code>b<\/code>. But 3 and 4 are not in <code>a<\/code>, so <code>b<\/code> is not a subset of <code>a<\/code>.<\/p>\n<p>Now here is a small detail that often confuses students.<\/p>\n<h2>Subset vs Proper Subset<\/h2>\n<p>A set is always a subset of itself. A <strong>proper subset<\/strong> is a subset that is <em>smaller<\/em> than the other set. Python uses <code>&lt;<\/code> for proper subsets:<\/p>\n<pre class=\"wp-block-code\"><code>c = {1, 2}\nprint(a.issubset(c), a &lt;= c, a &lt; c)\nprint(a &lt; b)<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>True True False\nTrue<\/code><\/pre>\n<p><code>a<\/code> and <code>c<\/code> are equal, so <code>a<\/code> is a subset of <code>c<\/code> but not a proper subset.<\/p>\n<h2>The Empty Set Is a Subset of Every Set<\/h2>\n<pre class=\"wp-block-code\"><code>print(set().issubset({5}))<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>True<\/code><\/pre>\n<p>This matches the rule from school mathematics: the empty set \u03c6 is a subset of every set.<\/p>\n<h2>issubset() with Lists and Strings<\/h2>\n<p>The method accepts any iterable, but the <code>&lt;=<\/code> operator needs sets on both sides:<\/p>\n<pre class=\"wp-block-code\"><code>print({1, 2}.issubset([1, 2, 3]))\ntry:\n    print({1, 2} &lt;= [1, 2, 3])\nexcept TypeError as e:\n    print(\"TypeError:\", e)\n\nprint(set(\"cat\").issubset(set(\"actions\")))\nprint(set(\"dog\").issubset(\"good\"))<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>True\nTypeError: '&lt;=' not supported between instances of 'set' and 'list'\nTrue\nTrue<\/code><\/pre>\n<p>So far we asked &#8220;is A inside B?&#8221; But sometimes you want to ask the question the other way round.<\/p>\n<h2>issuperset(): The Reverse Check<\/h2>\n<p>If <code>a<\/code> is a subset of <code>b<\/code>, then <code>b<\/code> is a <strong>superset<\/strong> of <code>a<\/code>:<\/p>\n<pre class=\"wp-block-code\"><code>print(b.issuperset(a), b &gt;= a)<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>True True<\/code><\/pre>\n<p>Read more: <a href=\"https:\/\/gyanipandit.com\/programming\/python-set-issuperset\/\">Python set issuperset()<\/a><\/p>\n<p>Let&#8217;s now use this in a small real-life example.<\/p>\n<h2>Practical Example: Checking Required Subjects<\/h2>\n<pre class=\"wp-block-code\"><code>required = {\"maths\", \"science\", \"english\"}\nchosen = {\"maths\", \"science\", \"english\", \"hindi\"}\nprint(required.issubset(chosen))<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>True<\/code><\/pre>\n<p>The student has chosen all the required subjects (plus one extra), so the check passes. The same pattern works for checking that a user has all required permissions or that a form has all required fields.<\/p>\n<h2>Practice Question<\/h2>\n<p>What does <code>print({1} &lt;= {1}, {1} &lt; {1})<\/code> print?<\/p>\n<p><strong>Answer:<\/strong> <code>True False<\/code>. A set is a subset of itself, but not a proper subset of itself.<\/p>\n<p>So friends, to sum up: <code>issubset()<\/code> returns <code>True<\/code> if every item of the first set is in the second, otherwise <code>False<\/code>. The <code>&lt;=<\/code> operator checks for a subset (equal sets count), while <code>&lt;<\/code> checks for a proper subset, which must be strictly smaller. And just like in mathematics, the empty set is a subset of every set, so <code>set().issubset(anything)<\/code> is always <code>True<\/code>. Try the practice question once more on your own, and this topic will be crystal clear.<\/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-isdisjoint\/\">isdisjoint()<\/a> \u00b7 <a href=\"https:\/\/gyanipandit.com\/programming\/python-set-union\/\">union()<\/a> \u00b7 <a href=\"https:\/\/gyanipandit.com\/programming\/python-set-intersection\/\">intersection()<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello friends! Remember subsets from your school mathematics? Python lets you check them in one line. The issubset() method checks whether every item of one set is also present in another set. If yes, the first set is a subset of the second, and the method returns True; otherwise it returns False. Let&#8217;s understand it &#8230; <a title=\"Python Set issubset() Method with Examples\" class=\"read-more\" href=\"https:\/\/gyanipandit.com\/programming\/python-set-issubset\/\" aria-label=\"Read more about Python Set issubset() 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-3405","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\/3405","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=3405"}],"version-history":[{"count":3,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3405\/revisions"}],"predecessor-version":[{"id":6140,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3405\/revisions\/6140"}],"wp:attachment":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media?parent=3405"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/categories?post=3405"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/tags?post=3405"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}