Python Set Intersection – The Ultimate Guide for Beginners

 

Python Set Intersection – The Ultimate Guide for Beginners


In the last week's article, you've learned what Python set 

difference()
 is all about. Now we'll explore another commonly used set function. Python set intersection allows you to find common elements between two or more sets. It has many useful applications, such as finding common skills between job applicants.

Today you'll learn everything there is about Python set intersection. You'll be ready to use it with confidence after reading the article.JNNC Technologies Pvt.Ltd 


Python Set Intersection – The Basics

So, what is Python set intersection? That's what we'll answer in this section. You'll get a complete understanding of the definition, syntax, and return values through visual examples.

Definition and usage

Set intersection function returns the common element(s) of two or more sets. For example, I like to code in Python, JavaScript, and PHP, and you use Java, Python, and Ruby. We both have Python in common. We'll work with two sets throughout the article just to keep things simple, but the same logic applies when intersecting more of them.


Image 3 – Set intersection as a Venn diagram (image by author)

Only Python is common to both sets, so I've highlighted the intersection area in blue.

What does intersection method do in Python and how do you find the intersection between sets? Let's go over the syntax to answer these questions.

Syntax

# Intersection of two sets
set1.intersection(set2)
# Intersection between multiple sets
set1.intersection(set2, set3)

Where:

  • set1
     – The iterable to find intersection from.
  • set2
    set3
     – Other sets use to calculate intersection.

Return value

The intersection function returns a new set which is the intersection between the first set and all other sets passed as arguments – but only if set(s) or iterable object(s) were passed to the function.

If no arguments were passed into the 

intersection()
 function, a copy of the set is returned.

Python Set Intersection Function Example

We'll declare two sets, just as on Image 1:

  • A
    : Contains PythonJavaScript, and PHP
  • B
    : Contains JavaPython, and Ruby

Only Python is present in both sets, so calculating an intersection should return a new set with Python being the only element:

A = {'Python', 'JavaScript', 'PHP'}
B = {'Java', 'Python', 'Ruby'}
print(f"A ∩ B = {A.intersection(B)}")

Output:

A ∩ B = {'Python'}

If you don't specify any parameters to the intersection function, a copy of the set is returned:

print(f"A ∩ / = {A.intersection()}")

Output:

A ∩ / = {'JavaScript', 'PHP', 'Python'}

You can verify it was copied by printing the memory address:

A = {'Python', 'JavaScript', 'PHP'}
A_copy = A.intersection()
print(hex(id(A)))
print(hex(id(A_copy)))


Post a Comment

0 Comments

'; (function() { var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true; dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js'; (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); })();