{"id":103,"date":"2024-01-25T10:36:11","date_gmt":"2024-01-25T10:36:11","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=103"},"modified":"2024-01-25T10:36:12","modified_gmt":"2024-01-25T10:36:12","slug":"python-issubset","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/01\/25\/python-issubset\/","title":{"rendered":"Python issubset"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you\u2019ll learn about how to use the Python\u00a0<code>issubset()<\/code>\u00a0method to check if a\u00a0set\u00a0is a subset of another set.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to the Python issubset() method<\/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\n<figure class=\"wp-block-image\"><img decoding=\"async\" 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\"\/><\/figure>\n\n\n\n<p>Set A and set B can be equal. If set A and set B are&nbsp;<strong>not<\/strong>&nbsp;equal, A is a&nbsp;<strong>proper<\/strong>&nbsp;subset of B.<\/p>\n\n\n\n<p>In Python, you can use the Set\u00a0<code>issubset()<\/code>\u00a0method to check if a set is a subset of another:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>set_a.issubset(set_b)<\/code><small>Code language: CSS (css)<\/small><\/code><\/pre>\n\n\n\n<p>If the&nbsp;<code>set_a<\/code>&nbsp;is a subset of the&nbsp;<code>set_b<\/code>, the&nbsp;<code>issubset()<\/code>&nbsp;method returns&nbsp;<code>True<\/code>. Otherwise, it returns&nbsp;<code>False<\/code>.<\/p>\n\n\n\n<p>The following example uses the\u00a0<code>issubset()<\/code>\u00a0method to check if the\u00a0<code>set_a<\/code>\u00a0is a subset of the\u00a0<code>set_b<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>numbers = {1, 2, 3, 4, 5} scores = {1, 2, 3} print(scores.issubset(numbers))<\/code><small>Code language: PHP (php)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>True<\/code><small>Code language: PHP (php)<\/small><\/code><\/pre>\n\n\n\n<p>By definition, a set is also a subset of itself. The following example returns\u00a0<code>True<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>numbers = {1, 2, 3, 4, 5} print(numbers.issubset(numbers))<\/code><small>Code language: PHP (php)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>True<\/code><small>Code language: PHP (php)<\/small><\/code><\/pre>\n\n\n\n<p>The following example returns\u00a0<code>False<\/code>\u00a0because some elements in the\u00a0<code>numbers<\/code>\u00a0set aren\u2019t in the\u00a0<code>scores<\/code>\u00a0set. In other words, the\u00a0<code>numbers<\/code>\u00a0set is not a subset of the\u00a0<code>scores<\/code>\u00a0set:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>numbers = {1, 2, 3, 4, 5} scores = {1, 2, 3} print(numbers.issubset(scores))<\/code><small>Code language: PHP (php)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>False<\/code><small>Code language: PHP (php)<\/small><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Using subset operators<\/h2>\n\n\n\n<p>Besides using the&nbsp;<code>issubset()<\/code>&nbsp;method, you can use the subset operator (<code>&lt;=<\/code>) to check if a set is a subset of another set:<code>set_a &lt;= set_b<\/code><\/p>\n\n\n\n<p>The subset operator (<code>&lt;=<\/code>) returns\u00a0<code>True<\/code>\u00a0if\u00a0<code>set_a<\/code>\u00a0is a subset of the\u00a0<code>set_b<\/code>. Otherwise, it returns\u00a0<code>False<\/code>. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>numbers = {1, 2, 3, 4, 5} scores = {1, 2, 3} result = scores &lt;= numbers print(result) <em># True<\/em> result = numbers &lt;= numbers print(result) <em># True<\/em><\/code><small>Code language: PHP (php)<\/small><\/code><\/pre>\n\n\n\n<p>The proper subset operator (<code>&lt;<\/code>) check if the&nbsp;<code>set_a<\/code>&nbsp;is a proper subset of the&nbsp;<code>set_b<\/code>:<code>set_a &lt; set_b<\/code><\/p>\n\n\n\n<p>For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>numbers = {1, 2, 3, 4, 5} scores = {1, 2, 3} result = scores &lt; numbers print(result) <em># True<\/em> result = numbers &lt; numbers print(result) <em># False<\/em><\/code><small>Code language: PHP (php)<\/small><\/code><\/pre>\n\n\n\n<p>In this example, the set&nbsp;<code>numbers<\/code>&nbsp;is not a proper subset of itself, therefore, the&nbsp;<code>&lt;<\/code>&nbsp;operator returns&nbsp;<code>False<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you\u2019ll learn about how to use the Python\u00a0issubset()\u00a0method to check if a\u00a0set\u00a0is a subset of another set. Introduction to the Python issubset() method 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, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21],"tags":[],"class_list":["post-103","post","type-post","status-publish","format-standard","hentry","category-7-sets"],"_links":{"self":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/103","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/comments?post=103"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/103\/revisions"}],"predecessor-version":[{"id":104,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/103\/revisions\/104"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=103"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=103"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=103"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}