{"id":3402,"date":"2026-11-16T11:51:15","date_gmt":"2026-11-16T11:51:15","guid":{"rendered":"https:\/\/gyanipandit.com\/programming\/?p=3402"},"modified":"2026-11-16T11:51:15","modified_gmt":"2026-11-16T11:51:15","slug":"python-set-isdisjoint","status":"publish","type":"post","link":"https:\/\/gyanipandit.com\/programming\/python-set-isdisjoint\/","title":{"rendered":"Python Set isdisjoint() Method with Examples"},"content":{"rendered":"<p>Hello friends! Sometimes the only thing you want to know about two groups is: do they have anything in common at all? For that, Python gives us <code>isdisjoint()<\/code>. The <code>isdisjoint()<\/code> method checks whether two sets have <strong>no items in common<\/strong>. Sets with nothing in common are called <em>disjoint<\/em>. The method returns <code>True<\/code> if there is no common item and <code>False<\/code> if there is at least one. Let&#8217;s see how it works.<\/p>\n<pre class=\"wp-block-code\"><code>a.isdisjoint(b)<\/code><\/pre>\n<h2>Basic Example<\/h2>\n<pre class=\"wp-block-code\"><code>x = {1, 2, 3}\ny = {4, 5}\nz = {3, 6}\nprint(x.isdisjoint(y))\nprint(x.isdisjoint(z))<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>True\nFalse<\/code><\/pre>\n<p><code>x<\/code> and <code>y<\/code> share nothing, so they are disjoint. <code>x<\/code> and <code>z<\/code> share the number 3, so they are not.<\/p>\n<h2>With Lists and Other Iterables<\/h2>\n<pre class=\"wp-block-code\"><code>print(x.isdisjoint([7, 8]))<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>True<\/code><\/pre>\n<p>Unlike most set operators, there is no operator form of <code>isdisjoint()<\/code>; you always call the method, and it accepts any iterable.<\/p>\n<p>Here is a small puzzle: what happens with two empty sets?<\/p>\n<h2>Two Empty Sets<\/h2>\n<pre class=\"wp-block-code\"><code>print(set().isdisjoint(set()))<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>True<\/code><\/pre>\n<p>Empty sets have nothing in common, so they are disjoint, even with each other.<\/p>\n<p>You might be thinking, why not just find the intersection and check if it is empty? You can, but there is a better way.<\/p>\n<h2>isdisjoint() vs Checking the Intersection<\/h2>\n<pre class=\"wp-block-code\"><code>print(len(x &amp; y) == 0)<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>True<\/code><\/pre>\n<p>This gives the same answer, but it builds a whole new set first. <code>isdisjoint()<\/code> stops as soon as it finds one common item, so it is cleaner and usually faster, especially for large sets. See also <a href=\"https:\/\/gyanipandit.com\/programming\/python-set-intersection\/\"><code>intersection()<\/code><\/a>.<\/p>\n<p>Now let&#8217;s use <code>isdisjoint()<\/code> in a few practical examples.<\/p>\n<h2>Practical Examples<\/h2>\n<h3>1. Do Two Batches Share Any Student?<\/h3>\n<pre class=\"wp-block-code\"><code>morning = {\"Riya\", \"Aman\"}\nevening = {\"Neha\", \"Kabir\"}\nprint(morning.isdisjoint(evening))<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>True<\/code><\/pre>\n<h3>2. Time-Slot Clash Check<\/h3>\n<pre class=\"wp-block-code\"><code>booked = {9, 10, 11}\nrequest = {12, 13}\nprint(\"Free\" if booked.isdisjoint(request) else \"Clash\")\nrequest = {11, 12}\nprint(\"Free\" if booked.isdisjoint(request) else \"Clash\")<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>Free\nClash<\/code><\/pre>\n<h3>3. Blocking Reserved Usernames<\/h3>\n<pre class=\"wp-block-code\"><code>banned = {\"admin\", \"root\"}\nfor name in [\"riya\", \"admin\"]:\n    print(name, \"ok\" if banned.isdisjoint({name}) else \"not allowed\")<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>riya ok\nadmin not allowed<\/code><\/pre>\n<h2>Practice Question<\/h2>\n<p>What does <code>print({2, 4}.isdisjoint({1, 3, 5}))<\/code> print?<\/p>\n<p><strong>Answer:<\/strong> <code>True<\/code>. The even numbers and the odd numbers have nothing in common.<\/p>\n<p>So friends, remember: two sets are disjoint when they have no items in common. There is no operator for <code>isdisjoint()<\/code>; you can check <code>not (a &amp; b)<\/code> instead, but that is less efficient. And like most set methods, it accepts any iterable, such as a list, tuple or string. Whenever your question is simply &#8220;do these share anything?&#8221;, reach for <code>isdisjoint()<\/code>.<\/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-issubset\/\">issubset()<\/a> \u00b7 <a href=\"https:\/\/gyanipandit.com\/programming\/python-set-intersection\/\">intersection()<\/a> \u00b7 <a href=\"https:\/\/gyanipandit.com\/programming\/python-set-difference\/\">difference()<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello friends! Sometimes the only thing you want to know about two groups is: do they have anything in common at all? For that, Python gives us isdisjoint(). The isdisjoint() method checks whether two sets have no items in common. Sets with nothing in common are called disjoint. The method returns True if there is &#8230; <a title=\"Python Set isdisjoint() Method with Examples\" class=\"read-more\" href=\"https:\/\/gyanipandit.com\/programming\/python-set-isdisjoint\/\" aria-label=\"Read more about Python Set isdisjoint() 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-3402","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\/3402","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=3402"}],"version-history":[{"count":3,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3402\/revisions"}],"predecessor-version":[{"id":6141,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3402\/revisions\/6141"}],"wp:attachment":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media?parent=3402"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/categories?post=3402"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/tags?post=3402"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}