Python Set intersection() Explained

The set intersection() method in Python is used to get the common elements present in two or multiple Sets. Alternatively, you can also use the Set operator & to perform the intersection that gives you the same result.

1. Quick Examples of Intersection Python Set

Below are quick examples of getting common elements from the Python Sets.


# Create three sets
id={1,2,3,4,5}
cost={1,2,45,67,54}
quantity={45,67,1,2,3}

# Intersection two sets
result = id.intersection(quantity)

# Intersection multiple sets
result = id.intersection(cost,quantity

# Intersection using & operator
result = id & cost

# Intersection multiple sets
result = id & cost & quantity

2. Python set intersection()

The intersection() method is used to get the elements that are in common from two or multiple sets. It will take single/multiple sets as parameters and it returns the new set with the common elements.

2.1 Syntax of set intersection()

The following is the syntax of the intersection() function.


# Syntax of intersection
set1.intersection(set2,set3,.....)

Let’s see some examples to understand this concept better

2.2 set intersection() Example

In this example, I will create three sets with integer elements and by using Python intersection(), will merge them into a single set object. To create a set in Python use {}.


# Create three sets
id={1,2,3,4,5}
cost={1,2,45,67,54}
quantity={45,67,1,2,3}

# Actual elements in the sets.
print("Set id :",id)
print("Set cost :",cost)
print("Set quantity :",quantity)

# Output:
# Set id : {1, 2, 3, 4, 5}
# Set cost : {1, 2, 67, 45, 54}
# Set quantity : {1, 2, 3, 67, 45}

Let’s perform the intersection operation on these set objects.


# Perform intersection on id and cost
print("id ∩ cost - ",id.intersection(cost))

# Perform intersection on id and quantity
print("id ∩ quantity - ",id.intersection(quantity))

# Perform intersection on id, cost and quantity
print("id ∩ cost ∩ quantity - ",id.intersection(cost,quantity))

# Output:
# id ∩ cost -  {1, 2}
# id ∩ quantity -  {1, 2, 3}
# id ∩ cost ∩ quantity -  {1, 2}

Explanation:

  1. We returned only the common elements from id and cost sets.
  2. We returned only the common elements from id and quantity sets.
  3. Finally we are also returning only the common elements that exists in all 3 sets.

3. Set intersection() with String Values

In this example, we will create three Python set objects with string elements and by using intersection() return only common elements among the sets.


# Create four sets
village1={"fields","plants","electricity","houses"}
village2={"fields","plants","electricity","houses","buildings"}
village3={"None"}

# Actual elements in the sets.
print("Set-1",village1)
print("Set-2",village2)
print("Set-3",village3)

# Output
# Set-1 {'electricity', 'fields', 'houses', 'plants'}
# Set-2 {'electricity', 'fields', 'buildings', 'houses', 'plants'}
# Set-3 {'None'}

Let’s perform set intersection on this set of strings.


# Perform intersection on village1 and village2
print("village1 ∩ village2 - ",village1.intersection(village2))

# Perform intersection on village2 and village3
print("village2 ∩ village3 - ",village2.intersection(village3))

# Output:
# village1 ∩ village2 -  {'houses', 'electricity', 'plants', 'fields'}
# village2 ∩ village3 -  set()

Explanation:

  1. There are 4 common strings present in village1 and village2.
  2. There are no common elements present in village2 and village3, So empty set is returned.

4. Python Set Intersection using & operator

We can also perform set intersection using the & operator in Python. This we can call an “and” operator. Let’s see some examples to understand this concept better.

4.1 Using & with Examples

In this example, we will create three sets with integer elements and return the common elements among these sets.


# Perform intersection on id and cost
print("id & cost - ",id &cost)

# Perform intersection on id and quantity
print("id & quantity - ",id & quantity)

# Perform intersection on id, cost and quantity
print("id & cost & quantity - ",id&cost&quantity)

# Output
# id & cost -  {1, 2}
# id & quantity -  {1, 2, 3}
# id & cost & quantity -  {1, 2}

Explanation:

  1. We returned only the common elements from id and cost sets.
  2. We returned only the common elements from id and quantity sets.
  3. Finally we are also returning only the common elements that exists in all 3 sets.

4.2 Using & with Examples


# Perform intersection on village1 and village2
print("village1 & village2 - ",village1 &village2)

# Perform intersection on village2 and village3
print("village2 & village3 - ",village2&village3)

# Output
# village1 & village2 -  {'houses', 'electricity', 'plants', 'fields'}
# village2 & village3 -  set()

Explanation:

  1. There are 4 common strings present in village1 and village2.
  2. There are no common elements present in village2 and village3, So an empty set is returned.

5. Differences between intersection() and & operator

Though both intersection() and & operator are used to return only the common elements that exist in sets in Python, they both have some differences. Below I have covered some differences.

INTERSECTION()& OPERATOR
intersection() is a method& is an operator
intersection() method explicitly fix the priority of the sets& operator will have specific operator precedence of the sets
intersection() is used only with sets& operator can be used with other data structures

6. Conclusion

In this article, you have learned how to return the common elements among the Python Sets using intersection() function and & operator. You can use intersection() and & operator to apply intersection with two or multiple sets.

Leave a Reply

You are currently viewing Python Set intersection() Explained