Python Set Intersection of sets and lists

Python Set

In this article, We are going to learn How Python Set Intersection() function works?. We will learn everything about Set Intersection() function. We will learn the Python Set Intersection of sets and lists in multiple ways.

So let us begin with Intersection() function and see what is Intersection() set function.

Python Set Intersection() Method


The Intersection() set method returns a new set of all the elements common in all given Sets. We can achieve the intersection of Sets using Intersection() or & operator. These are two ways that we can use to take the intersection.

# Syntax for Intersection()

#Syntax for Intersection() 
set.Intersection(*others)

Parameters of Intersection()

  • Others: It can be a set or iterables (tuple, list, dictionary, string).
  • * it represents multiple arguments to the intersection() method.
  • If no argument is passed then it returns a shallow copy of the set.

Let us understand more with the help of examples of set intersections.

Python Find intersections of two Sets


In this code example, we are finding the intersection of two sets Using the intersection() method.

Num_set = {12,16,'kiwi'}

fruit_set = {'apple','kiwi',16,12}
animal_set = {'Dog','Cat','Bunny',12,'kiwi',16}


#instersection of two sets using intersection()
Two_set_inserection = Num_set.intersection(fruit_set)

print('two sets using intersection method :\n ',Two_set_inserection)



Output

two sets using intersection method :
  {16, 'kiwi', 12}

Python Find intersections of Multiple Sets


In this example, we are finding the intersection of multiple sets using the set intersection() method.

Num_set = {12,16,'kiwi'}

fruit_set = {'apple','kiwi',16,12}
animal_set = {'Dog','Cat','Bunny',12,'kiwi',16}
#instersection of multiple sets using intersection()

inserection_set = Num_set.intersection(fruit_set,animal_set)

print('mutiples sets intersection :\n ',Two_set_inserection)

Output

mutiples sets intersection:
  {16, 'kiwi', 12}

Find intersections of two Sets using & operator


Here is another example, which is using the & operator to take the intersection of Python sets.

Num_set = {12,16,'kiwi'}

fruit_set = {'apple','kiwi',16,12}
animal_set = {'Dog','Cat','Bunny',12,'kiwi',16}


#instersection of two sets using intersection()

Two_set_inserection = Num_set & fruit_set

print('two sets using & operator :\n ',Two_set_inserection)



Output

two sets using & operator :
  {16, 12, 'kiwi'}

Find intersection of multiple sets with & opertaor


In this example, we are finding the intersection of multiple sets using intersection() method.

Num_set = {12,16,'kiwi'}

fruit_set = {'apple','kiwi',16,12}
animal_set = {'Dog','Cat','Bunny',12,'kiwi',16}


mul_set_inserectiont = Num_set & fruit_set & animal_set

print('mutiples sets using using & operator :\n ',Two_set_inserection)

Output

multiples sets using using & operator :
  {16, 12, 'kiwi'}

Python intersections of multiple lists


In this example we have three lists(animal_list,fruits_list,num_list),so are finding the intersection of all these multiple lists.

Num_set = {12,16,'kiwi',25}
animal_list = ['Dog',16, 'kiwi',25]
fruits_list = ['Apple',20,'kiwi',25]
num_list = [12,13,14,25,'kiwi']

result_set = Num_set.intersection(animal_list,fruits_list,num_list)

print(result_set )


Output

{25, 'kiwi'}

Python Set order intersection of two list


In this example, we are intersecting two lists while painting the order of elements after the intersection.


animal_list = ['Dog',16, 'kiwi',25]
fruits_list = ['Apple',20,'kiwi',25]


set = frozenset(animal_list)
result = [item for item in fruits_list if x in set]
print(result)

Output

['kiwi', 25]

Python intersections of Single or multiple iterables

In this example, we are taking intersection of single or multiple iterables using the intersection() method. In this example we will be taking intersection with a Python dictionary & list.

Num_set = {12,16,'kiwi',15}

#define a tuple
fruit_tup = ('apple','kiwi',16)

# define a list
animal_list = ['kiwi',16]

#define a dictionary
dict = {'A':65,'B':66,'kiwi':16}

#single iterable  intersection
inserection_set = Num_set.intersection(animal_list)

# multipul iterable list,tuple,dict intersection
Muti_iter_inserct = Num_set.intersection(animal_list,fruit_tup,dict)

print('single iterable intersection :\n ',inserection_set)
print('multiple iterables intersection  :\n ',Muti_iter_inserct)

Output

single iterable intersection : 
  {'kiwi', 16}
multiple iterables intersections:
  {'kiwi'}

Conclusion

In this article, we learned about the Intersection function and & Operator that helps us to take the common items which are common among the iterables. These ways are helpful when we want to find common data between many datasets.

We hope you will use these Python Sets Intersection techniques in your programs.