{"id":638,"date":"2020-10-18T08:11:58","date_gmt":"2020-10-18T08:11:58","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=638"},"modified":"2025-03-27T03:05:49","modified_gmt":"2025-03-27T03:05:49","slug":"python-issubset","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/python-basics\/python-issubset\/","title":{"rendered":"Python issubset"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn about how to use the Python <code>issubset()<\/code> method to check if a <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-set\/\">set<\/a> is a subset of another set.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-python-issubset-method'>Introduction to the Python issubset() method <a href=\"#introduction-to-the-python-issubset-method\" class=\"anchor\" id=\"introduction-to-the-python-issubset-method\" title=\"Anchor for Introduction to the Python issubset() method\">#<\/a><\/h2>\n\n\n\n<p>Suppose that you have two sets A and B. Set A is a subset of set B if all elements of A are also elements of B. Then, set B is a superset of set A.<\/p>\n\n\n\n<p>The following Venn diagram illustrates that set A is a subset of the set B:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"230\" height=\"230\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/10\/Python-issubset.png\" alt=\"Python issubset: Set A is subset of Set B\" class=\"wp-image-640\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/10\/Python-issubset.png 230w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/10\/Python-issubset-150x150.png 150w\" sizes=\"auto, (max-width: 230px) 100vw, 230px\" \/><\/figure>\n<\/div>\n\n\n<p>Set A and set B can be equal. If set A and set B are <strong>not<\/strong> equal, A is a <strong>proper<\/strong> subset of B.<\/p>\n\n\n\n<p>In Python, you can use the Set <code>issubset()<\/code> method to check if a set is a subset of another:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">set_a<\/span><span class=\"hljs-selector-class\">.issubset<\/span>(<span class=\"hljs-selector-tag\">set_b<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>If the <code>set_a<\/code> is a subset of the <code>set_b<\/code>, the <code>issubset()<\/code> method returns <code>True<\/code>. Otherwise, it returns <code>False<\/code>.<\/p>\n\n\n\n<p>The following example uses the <code>issubset()<\/code> method to check if the <code>set_a<\/code> is a subset of the <code>set_b<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">numbers = {<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>}\nscores = {<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>}\n\n<span class=\"hljs-keyword\">print<\/span>(scores.issubset(numbers))<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=bnVtYmVycyA9IHsxLCAyLCAzLCA0LCA1fQpzY29yZXMgPSB7MSwgMiwgM30KCnByaW50KHNjb3Jlcy5pc3N1YnNldChudW1iZXJzKSk%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-keyword\">True<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>By definition, a set is also a subset of itself. The following example returns <code>True<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">numbers = {<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>}\n\n<span class=\"hljs-keyword\">print<\/span>(numbers.issubset(numbers))<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=bnVtYmVycyA9IHsxLCAyLCAzLCA0LCA1fQoKcHJpbnQobnVtYmVycy5pc3N1YnNldChudW1iZXJzKSk%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-keyword\">True<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following example returns <code>False<\/code> because some elements in the <code>numbers<\/code> set aren&#8217;t in the <code>scores<\/code> set. In other words, the <code>numbers<\/code> set is not a subset of the <code>scores<\/code> set:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">numbers = {<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>}\nscores = {<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>}\n\n<span class=\"hljs-keyword\">print<\/span>(numbers.issubset(scores))<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=bnVtYmVycyA9IHsxLCAyLCAzLCA0LCA1fQpzY29yZXMgPSB7MSwgMiwgM30KCnByaW50KG51bWJlcnMuaXNzdWJzZXQoc2NvcmVzKSk%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-keyword\">False<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='using-subset-operators'>Using subset operators <a href=\"#using-subset-operators\" class=\"anchor\" id=\"using-subset-operators\" title=\"Anchor for Using subset operators\">#<\/a><\/h2>\n\n\n\n<p>Besides using the <code>issubset()<\/code> method, you can use the subset operator (<code>&lt;=<\/code>) to check if a set is a subset of another set:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">set_a &lt;= set_b<\/code><\/span><\/pre>\n\n\n<p>The subset operator (<code>&lt;=<\/code>) returns <code>True<\/code> if <code>set_a<\/code> is a subset of the <code>set_b<\/code>. Otherwise, it returns <code>False<\/code>. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">numbers = {<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>}\nscores = {<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>}\n\nresult = scores &lt;= numbers\n<span class=\"hljs-keyword\">print<\/span>(result)  <span class=\"hljs-comment\"># True<\/span>\n\nresult = numbers &lt;= numbers\n<span class=\"hljs-keyword\">print<\/span>(result)  <span class=\"hljs-comment\"># True<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=bnVtYmVycyA9IHsxLCAyLCAzLCA0LCA1fQpzY29yZXMgPSB7MSwgMiwgM30KCnJlc3VsdCA9IHNjb3JlcyA8PSBudW1iZXJzCnByaW50KHJlc3VsdCkgICMgVHJ1ZQoKcmVzdWx0ID0gbnVtYmVycyA8PSBudW1iZXJzCnByaW50KHJlc3VsdCkgICMgVHJ1ZQ%3D%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>The proper subset operator (<code>&lt;<\/code>) check if the <code>set_a<\/code> is a proper subset of the <code>set_b<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">set_a &lt; set_b<\/code><\/span><\/pre>\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">numbers = {<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>}\nscores = {<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>}\n\nresult = scores &lt; numbers\n<span class=\"hljs-keyword\">print<\/span>(result)  <span class=\"hljs-comment\"># True<\/span>\n\nresult = numbers &lt; numbers\n<span class=\"hljs-keyword\">print<\/span>(result)  <span class=\"hljs-comment\"># False<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this example, the set <code>numbers<\/code> is not a proper subset of itself, therefore, the <code>&lt;<\/code> operator returns <code>False<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Set <code>A<\/code> is a subset of set <code>B<\/code> if all elements of the set <code>A<\/code> are also elements of the set <code>B<\/code><\/li>\n\n\n\n<li>Use Set <code>issubset()<\/code> method returns <code>True<\/code> if a set is a subset of another set.<\/li>\n\n\n\n<li>Also, use the subset operator (&lt;=)  or the proper subset operator (&lt;) to check if a set is a subset or proper subset of another set.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='quiz'>Quiz <a href=\"#quiz\" class=\"anchor\" id=\"quiz\" title=\"Anchor for Quiz\">#<\/a><\/h2>\n\n\n\n<iframe loading=\"lazy\"\n  name=\"quiz\"\n  src=\"\/quiz\/?quiz=issubset\"\n  height=\"700\"\n  width=\"600\"\n  class=\"iframe\"\n><\/iframe>\n\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"638\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-basics\/python-issubset\/\"\n\t\t\t\tdata-post-title=\"Python issubset\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"638\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-basics\/python-issubset\/\"\n\t\t\t\tdata-post-title=\"Python issubset\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn about how to use the Python issubset() method to check if a set is a subset of another set.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":37,"menu_order":46,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-638","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/638","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/comments?post=638"}],"version-history":[{"count":2,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/638\/revisions"}],"predecessor-version":[{"id":7073,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/638\/revisions\/7073"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/37"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=638"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}