Python

Intersection method in set

Click for All Topics

Intersection method in set - Python

  • This method returns a new set containing all common elements from all of the set and all elements are unique in the new set.
  • If there are no common elements among all the set it returns an empty set.
  • This method can take zero to any number of collection or iterable types (e.g. list, tuple, dictionary, set, string) of data as an argument.
  • When you call the intersection method with a set and pass one set as an argument. first it creates a new set(result set) and then finds the common elements if the element is common in both sets then it adds that element to the new set(result set).
  • After traversing all the elements it returns the new set(result set).
  • If we call the intersection method with a zero argument then it returns the shallow copy of the set(the set which is called with the intersection).
  • It returns an empty if there are no common elements in the sets.
  • You can also use & operator in place of .intersection() method.

Syntax –

set_name.intersection(iterable1,iterable2,iterable3,...)

Parameters –

  • iterable → any number of collections or iterable elements.
  • return value → returns a new set.

Example –

				
					set1={1,2,3,4,5}
set2={3,4,5,6,7}
print("Set 1:",set1)
print("Set 2:",set2)
s=set1.intersection(set2)
print("Resultant set:",s)

# Output -
# Set 1: {1, 2, 3, 4, 5}
# Set 2: {3, 4, 5, 6, 7}
# Resultant set: {3, 4, 5}
				
			
  • In the above example ,first we create two sets set1, set2 with elements.
  • After creating sets print both the sets on the output screen.
  • After that, we call the intersection method with set1 pass set2 as an argument and store the return value in variable s.
  • print the resultant set s on the output screen.
  • Here we can also change the position of set1 to set2.

Intersection method with no argument –

Example –

				
					s={'Hello','Everyone','this','is','edSlash'}
print("Set 1:",s)
s1=s.intersection(s1)
print("Resultant set:",s1)

# Output -
# Set 1: {'Hello','Everyone','this','is','edSlash'}
# Resultant set: {'Hello','Everyone','this','is','edSlash'}
				
			

Intersection with two arguments or intersection of three sets –

Example –

				
					s={11,12,13,14,15}
s1={12,14,16,11}
s2={11,13,14}
print(s)
print(s1)
print(s2)
resultant_set=s.intersection(s1,s2)
print("resultant set:",resultant_set)

# Output -
# {11, 12, 13, 14, 15}
# {16, 11, 12, 14}
# {11, 13, 14}
# resultant set: {11, 14}
				
			

Intersection with no common elements –

Example –

				
					my_set1={10,20,30,40,50}
my_set2={'A','B','C','D','E'}
print("Set 1:",my_set1)
print("Set 2:",my_set2)
s=my_set1.intersection(my_set2)
print("Resultant set:",s)

# Output -
# Set 1: {50, 20, 40, 10, 30}
# Set 2: {'A', 'C', 'D', 'E', 'B'}
# Resultant set: set()
				
			

Intersection with another type of iterable or collection element e.g. list, tuple, dictionary, string-

Example –

				
					my_set={'a','b','c','f','g','h'}
my_list=['a','b','c','d','e']
my_tuple=('a','b','c')
my_dict={'a':12,'b':15}
my_string='ab'
returned_set=my_set.intersection(my_list,my_tuple,my_dict,my_string)
print('set:',my_set)
print('list:',my_list)
print('tuple:',my_tuple)
print('dictionary:',my_dict)
print('string:',my_string)
print('Resultant set:',returned_set)

# Output -
# set: {'c', 'h', 'f', 'a', 'b', 'g'}
# list: ['a', 'b', 'c', 'd', 'e']
# tuple: ('a', 'b', 'c')
# dictionary: {'a': 12, 'b': 15}
# string: ab
# Resultant set: {'a', 'b'}
				
			

Intersection using & operator –

Example –

				
					Science_stream_subject={'Hindi','English','Math','Physics','Chemistry'}
Commerce_stream_subject={'Hindi','English','Accountancy','Business Studies','Economics'}
print("Science stream subject:",Science_stream_subject)
print("Commerce stream subject:",Commerce_stream_subject)
common_subject=Commerce_stream_subject & Science_stream_subject
print("Common subject:",common_subject)

# Output -
# Science stream subject: {'Hindi', 'English', 'Chemistry', 'Math', 'Physics'}
# Commerce stream subject: {'Business Studies', 'Economics', 'English', 'Accountancy', 'Hindi'}
# Common subject: {'Hindi', 'English'}