.intersection()
The .intersection()
method returns a new set with objects that exist inside two or more sets. It’s a fundamental set operation that mirrors the mathematical concept of intersection. This method is widely used in data analysis, filtering datasets, and identifying shared elements between collections. Its ability to operate on multiple sets at once makes it a concise and efficient tool for comparing groups of data.
Here is a Venn diagram that shows how the .intersection()
method finds the intersection of multiple sets:
Syntax
set.intersection(set1, set2, ..., setN)
Alternatively:
set & set1 & set2 ... & setN
Parameters:
set
: The input set in which to search for common values.set1, set2, ..., setN
: The other sets in which to search for common values.
Return Value:
The .intersection()
method returns a new set with values that exist inside the given sets.
Example 1: Using .intersection()
with Two Sets
In this example, a set of mutual_friends
is created with the .intersection()
method using two sets of friend groups:
# Create two sets of friend groupsfriend_group_1 = {'Juan', 'Mary', 'David', 'Yukiko', 'Alice'}friend_group_2 = {'Yukiko', 'Peter', 'Juan', 'Alice'}# Find the mutual friends in these setsmutual_friends = friend_group_1.intersection(friend_group_2)# Print the resultprint(mutual_friends)
The output for the example will be:
{'Juan', 'Yukiko', 'Alice'}
Example 2: Using .intersection()
with Three or More Sets
In this example, the .intersection()
method finds the intersection of three sets:
# Create three setsbase_set = {1, 2, 3, 4}base_set1 = {2, 3, 5}base_set2 = {0, 2, 3, 7}# Find the intersection of these setsres = base_set.intersection(base_set1, base_set2)# Print the resultprint(res)
The output for the example will be:
{2, 3}
Codebyte Example: Using .intersection()
with Non-Set Iterables
In this codebyte example, the .intersection()
method converts all the non-set iterables to sets and then finds the intersection of all the sets:
Frequently Asked Questions
1. Can I chain multiple .intersection()
calls?
Yes, you can chain .intersection()
calls to find the intersection of multiple sets or iterables. Each call returns a new set with the intersected result.
2. Does .intersection()
modify the original set?
No, .intersection()
returns a new set and does not change the original set. Use .intersection_update()
to modify the original.
3. What happens if I use .intersection()
with no arguments?
If used with no arguments, .intersection()
returns a shallow copy of the original set, which is a new set containing the same elements as the original.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours