{"id":3389,"date":"2026-11-14T12:13:08","date_gmt":"2026-11-14T12:13:08","guid":{"rendered":"https:\/\/gyanipandit.com\/programming\/?p=3389"},"modified":"2026-11-14T12:13:18","modified_gmt":"2026-11-14T12:13:18","slug":"python-set-intersection","status":"publish","type":"post","link":"https:\/\/gyanipandit.com\/programming\/python-set-intersection\/","title":{"rendered":"Python Set intersection() Method with Examples"},"content":{"rendered":"<p>Hello friends! Many times we need to find what two groups have in common. In Python, the easiest way to do this is with sets. The <code>intersection()<\/code> method returns a <strong>new set<\/strong> containing only the items that are present in <strong>all<\/strong> the sets you compare. It answers questions like &#8220;which students are in both the Maths club and the Science club?&#8221; The original sets are not changed. Let&#8217;s understand it with examples.<\/p>\n<pre class=\"wp-block-code\"><code>set1.intersection(set2, set3, ...)\nset1 &amp; set2 &amp; set3        # operator form<\/code><\/pre>\n<h2>Basic Example<\/h2>\n<pre class=\"wp-block-code\"><code>a = {1, 2, 3, 4}\nb = {3, 4, 5, 6}\nprint(a.intersection(b))\nprint(a &amp; b)\nprint(a)<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>{3, 4}\n{3, 4}\n{1, 2, 3, 4}<\/code><\/pre>\n<p>You can compare more than two sets at once as well.<\/p>\n<h2>Intersection of More Than Two Sets<\/h2>\n<pre class=\"wp-block-code\"><code>c = {4, 6, 8}\nprint(a.intersection(b, c))\nprint(a &amp; b &amp; c)<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>{4}\n{4}<\/code><\/pre>\n<p>Only <code>4<\/code> is present in all three sets.<\/p>\n<h2>With Lists, Tuples and Ranges<\/h2>\n<p>Like <code>union()<\/code>, the method accepts any iterable, while the <code>&amp;<\/code> operator needs sets on both sides:<\/p>\n<pre class=\"wp-block-code\"><code>print(a.intersection([2, 4, 9], range(4)))<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>{2}<\/code><\/pre>\n<p><code>range(4)<\/code> is 0, 1, 2, 3, so only 2 is common to <code>a<\/code>, the list and the range.<\/p>\n<p>But what happens if the sets have nothing in common? Let&#8217;s check.<\/p>\n<h2>When Nothing Is Common<\/h2>\n<pre class=\"wp-block-code\"><code>print({1, 2}.intersection({3, 4}))\nprint({1, 2}.isdisjoint({3, 4}))<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>set()\nTrue<\/code><\/pre>\n<p>An empty set prints as <code>set()<\/code> (not <code>{}<\/code>, which is an empty dictionary). If you only want to know <em>whether<\/em> two sets share anything, <a href=\"https:\/\/gyanipandit.com\/programming\/python-set-isdisjoint\/\"><code>isdisjoint()<\/code><\/a> is faster and clearer.<\/p>\n<h2>Changing a Set in Place: intersection_update() and &amp;=<\/h2>\n<pre class=\"wp-block-code\"><code>s = {1, 2, 3, 4}\ns.intersection_update({2, 4, 6})\nprint(s)\ns &amp;= {4}\nprint(s)<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>{2, 4}\n{4}<\/code><\/pre>\n<p>Read more: <a href=\"https:\/\/gyanipandit.com\/programming\/python-set-intersection_update\/\">Python set intersection_update()<\/a><\/p>\n<p>Now let&#8217;s use <code>intersection()<\/code> in a few practical situations.<\/p>\n<h2>Practical Examples<\/h2>\n<h3>1. Students in Both Clubs<\/h3>\n<pre class=\"wp-block-code\"><code>maths = {\"Riya\", \"Aman\", \"Kabir\", \"Neha\"}\nscience = {\"Aman\", \"Neha\", \"Zoya\"}\nprint(sorted(maths &amp; science))<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>['Aman', 'Neha']<\/code><\/pre>\n<h3>2. Common Letters in Two Words<\/h3>\n<pre class=\"wp-block-code\"><code>print(sorted(set(\"python\") &amp; set(\"typhoon\")))<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>['h', 'n', 'o', 'p', 't', 'y']<\/code><\/pre>\n<h3>3. Common Items of Two Lists, Keeping Order and Duplicates<\/h3>\n<p>Sets lose order and duplicates. If you need to keep them, use a set only for the fast lookup:<\/p>\n<pre class=\"wp-block-code\"><code>l1 = [5, 3, 8, 1, 3]\nl2 = [3, 1, 9]\nlookup = set(l2)\ncommon = [x for x in l1 if x in lookup]\nprint(common)<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>[3, 1, 3]<\/code><\/pre>\n<h2>Practice Question<\/h2>\n<p>Here is a small question for you. Try it before you read the answer:<\/p>\n<p>What does <code>print({1, 2, 3} &amp; {2, 3, 4} &amp; {3, 4, 5})<\/code> print?<\/p>\n<p><strong>Answer:<\/strong> <code>{3}<\/code>. Only 3 is in all three sets.<\/p>\n<p>So friends, to sum up: <code>intersection()<\/code> does not modify the original set, it returns a new one, and you use <code>intersection_update()<\/code> or <code>&amp;=<\/code> when you want to change the original. When there are no common items, you get an empty set, shown as <code>set()<\/code>. And remember the difference: intersection keeps only the items common to all sets, while <a href=\"https:\/\/gyanipandit.com\/programming\/python-set-union\/\">union<\/a> keeps every item from every set. Practise with your own examples and it will become second nature.<\/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-union\/\">union()<\/a> \u00b7 <a href=\"https:\/\/gyanipandit.com\/programming\/python-set-difference\/\">difference()<\/a> \u00b7 <a href=\"https:\/\/gyanipandit.com\/programming\/python-set-issubset\/\">issubset()<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello friends! Many times we need to find what two groups have in common. In Python, the easiest way to do this is with sets. The intersection() method returns a new set containing only the items that are present in all the sets you compare. It answers questions like &#8220;which students are in both the &#8230; <a title=\"Python Set intersection() Method with Examples\" class=\"read-more\" href=\"https:\/\/gyanipandit.com\/programming\/python-set-intersection\/\" aria-label=\"Read more about Python Set intersection() 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-3389","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\/3389","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=3389"}],"version-history":[{"count":3,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3389\/revisions"}],"predecessor-version":[{"id":6137,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3389\/revisions\/6137"}],"wp:attachment":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media?parent=3389"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/categories?post=3389"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/tags?post=3389"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}