Python Set intersection() Method – Tutorial with Examples

Python Set intersection() Method

A set is an unordered collection of unique elements. Python provides several built-in methods for manipulating sets, including the intersection() method. The intersection() method returns a new set that contains the common elements of two or more sets. In other words, it returns the intersection of two or more sets.

Syntax

set.intersection(set1, set2, set3, ...)

The intersection() method takes one or more sets as its arguments and returns a new set that contains only the elements that are common to all the sets. The sets can be passed as separate arguments or as elements of an iterable (e.g., a list, tuple, or another set).

If no arguments are passed to the intersection() method, it returns a new set that is the same as the original set.

Examples

Example 1: Using intersection() with two sets

Let’s create two sets and find their intersection using the intersection() method:

# Create two sets
set1 = {1, 2, 3, 4, 5}
set2 = {4, 5, 6, 7, 8}

# Find the intersection of set1 and set2
intersection = set1.intersection(set2)

# Print the intersection
print(intersection)

Output:

{4, 5}

In the above example, we created two sets (set1 and set2) and found their intersection using the intersection() method. The intersection contains only the common elements of the two sets (4 and 5).

Example 2: Using intersection() with three sets

Let’s create three sets and find their intersection using the intersection() method:

# Create three sets
set1 = {1, 2, 3, 4, 5}
set2 = {4, 5, 6, 7, 8}
set3 = {5, 6, 7, 8, 9}

# Find the intersection of set1, set2, and set3
intersection = set1.intersection(set2, set3)

# Print the intersection
print(intersection)

Output:

{5}

In the above example, we created three sets (set1, set2, and set3) and found their intersection using the intersection() method. The intersection contains only the common element of all three sets (5).

Example 3: Using intersection() with an empty set

Let’s create an empty set and find its intersection with another set using the intersection() method:

# Create an empty set
set1 = set()

# Create another set
set2 = {1, 2, 3}

# Find the intersection of set1 and set2
intersection = set1.intersection(set2)

# Print the intersection
print(intersection)

Output:

set()

In the above example, we created an empty set (set1) and another set (set2) and found their intersection using the intersection() method. Since the empty set has no elements, its intersection with any other set is also an empty set.

Use Cases

The intersection() method can be useful in a variety of scenarios. For example, it can be used to find common elements in two or more lists or to check if two sets have any common elements. It can also be used to filter data based on common attributes.

Here are some examples of use cases:

    • Checking if two sets have any common elements:
set1 = {1, 2, 3}
set2 = {3, 4, 5}
if set1.intersection(set2):
    print("The sets have common elements")
else:
    print("The sets do not have any common elements")
    • Finding common elements in multiple lists:
list1 = [1, 2, 3, 4, 5]
list2 = [4, 5, 6, 7, 8]
list3 = [5, 6, 7, 8, 9]
common_elements = set(list1).intersection(list2, list3)
print(common_elements)
    • Filtering a list based on common attributes:
class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age
person1 = Person("John", 30)
person2 = Person("Mary", 25)
person3 = Person("Jane", 30)
person4 = Person("Bob", 40)

people = [person1, person2, person3, person4]
common_ages = set([person.age for person in people]).intersection([30, 40])
filtered_people = [person for person in people if person.age in common_ages]
for person in filtered_people:
print(person.name)

In the above example, we created a list of Person objects, where each person has a name and an age. We used the intersection() method to find the common ages of the people and then filtered the list based on those ages. The output is a list of Person objects whose age is either 30 or 40.

The intersection() method is a powerful tool for working with sets in Python. It can be used to find common elements in two or more sets, to check if two sets have any common elements, or to filter data based on common attributes. With the help of the examples and use cases provided in this guide, you should be able to start using the intersection() method in your own Python code.

Leave a Reply

Your email address will not be published. Required fields are marked *