{"id":97,"date":"2024-01-25T10:28:04","date_gmt":"2024-01-25T10:28:04","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=97"},"modified":"2024-01-25T10:28:05","modified_gmt":"2024-01-25T10:28:05","slug":"python-set-intersection","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/01\/25\/python-set-intersection\/","title":{"rendered":"Python Set Intersection"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you\u2019ll learn about the Python set intersection and how to use it to intersect two or more sets.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">TL;DR<\/h2>\n\n\n\n<p>In Python, you can use the set\u00a0<code>intersection()<\/code>\u00a0method or set intersection operator (&amp;) to intersect two or more\u00a0sets:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>new_set = set1.intersection(set2, set3) new_set = set1 &amp; set2 &amp; set3<\/code><\/code><\/pre>\n\n\n\n<p>The intersection() method and &amp; operator have the same performance.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to Python set intersection<\/h2>\n\n\n\n<p>When intersecting two or more\u00a0sets, you\u2019ll get a new set consisting of elements that exist in all sets.<\/p>\n\n\n\n<p>Suppose that you have two following sets\u00a0<code>s1<\/code>\u00a0and\u00a0<code>s2<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>s1 = {'Python', 'Java','C++'} s2 = {'C#', 'Java', 'C++' }<\/code><small>Code language: JavaScript (javascript)<\/small><\/code><\/pre>\n\n\n\n<p>The intersection of these two sets returns a new set that contains two elements\u00a0<code>'Java'<\/code>\u00a0and\u00a0<code>'C++'<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>s = {'Java', 'C++'}<\/code><small>Code language: JavaScript (javascript)<\/small><\/code><\/pre>\n\n\n\n<p>\u2026 because they\u2019re the only elements that exist in both sets.<\/p>\n\n\n\n<p>The following Venn diagram illustrates the intersection of two sets&nbsp;<code>s1<\/code>&nbsp;and&nbsp;<code>s<\/code>2:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/10\/Python-Set-Intersection.png\" alt=\"Python Set Intersection Example\" class=\"wp-image-607\"\/><\/figure>\n\n\n\n<p>The set intersection has many useful applications. For example, you can use set intersections to find the common favorites of two friends on a social networking application or to search for common skills of two or more employees on an HR application.<\/p>\n\n\n\n<p>In Python, you can intersect two or more sets using the set&nbsp;<code>intersection()<\/code>&nbsp;method or set intersection operator (<code>&amp;<\/code>).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1) Using Python set intersection() method to intersect two or more sets<\/h3>\n\n\n\n<p>This example shows how to use the set\u00a0<code>intersection()<\/code>\u00a0method to intersect two or more sets:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>new_set = set1.intersection(set2, set3, ...)<\/code><\/code><\/pre>\n\n\n\n<p>The following shows how to use the\u00a0<code>intersection()<\/code>\u00a0method to intersect the sets s1 and s2:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>s1 = {'Python', 'Java', 'C++'} s2 = {'C#', 'Java', 'C++'} s = s1.intersection(s2) print(s) <\/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>{'C++', 'Java'} <\/code><small>Code language: JavaScript (javascript)<\/small><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2) Using Python set intersection (&amp;) operator to intersect two or more sets<\/h3>\n\n\n\n<p>Python provides you with the set intersection operator (<code>&amp;<\/code>) that allows you to intersect two or more sets:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>new_set = s1 &amp; s2 &amp; s3 &amp; ...<\/code><\/code><\/pre>\n\n\n\n<p>The following example uses the set intersection operator (<code>&amp;<\/code>) to intersect the sets s1 and s2:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>s1 = {'Python', 'Java', 'C++'} s2 = {'C#', 'Java', 'C++'} s = s1 &amp; s2 print(s)<\/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>new_set = s1 &amp; s2 &amp; s3 &amp; ...<\/code><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Set intersection() method vs set intersection operator (&amp;)<\/h3>\n\n\n\n<p>The set intersection operator only allows sets, while the set\u00a0<code>intersection()<\/code>\u00a0method can accept any\u00a0iterables, like\u00a0strings,\u00a0lists, and\u00a0dictionaries.<\/p>\n\n\n\n<p>If you pass iterables to the&nbsp;<code>intersection()<\/code>&nbsp;method, it\u2019ll convert the iterables to set before intersecting them.<\/p>\n\n\n\n<p>However, the set intersection operator (<code>&amp;<\/code>) will raise an error if you use it with iterables.<\/p>\n\n\n\n<p>The following example uses the\u00a0<code>intersection()<\/code>\u00a0method to intersect a set with a list:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>numbers = {1, 2, 3} scores = &#91;2, 3, 4] numbers = numbers.intersection(scores) print(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>{2, 3}<\/code><\/code><\/pre>\n\n\n\n<p>If you use the set intersection operator (<code>&amp;<\/code>) instead, you\u2019ll get an error:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>numbers = {1, 2, 3} scores = &#91;2, 3, 4] numbers = numbers &amp; scores print(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>TypeError: unsupported operand type(s) for &amp;: 'set' and 'list'<\/code><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you\u2019ll learn about the Python set intersection and how to use it to intersect two or more sets. TL;DR In Python, you can use the set\u00a0intersection()\u00a0method or set intersection operator (&amp;) to intersect two or more\u00a0sets: The intersection() method and &amp; operator have the same performance. Introduction to Python set intersection [&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-97","post","type-post","status-publish","format-standard","hentry","category-7-sets"],"_links":{"self":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/97","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=97"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/97\/revisions"}],"predecessor-version":[{"id":98,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/97\/revisions\/98"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=97"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=97"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=97"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}