Click for All Topics
-
operator in place of the .difference()
method.
Syntax –
set_name.difference(iterable1,iterable2,iterable3…)
Parameters –
Example –
A={1,2,3,4,5}
B={4,5,6,7,8}
s=A.difference(B)
print("Original set A:",s1)
print("Original set B:",s2)
print("Resultant set A-B :",s)
# Output -
# Original set A: {1, 2, 3, 4, 5}
# Original set B: {4, 5, 6, 7, 8}
# Resultant set A-B : {1, 2, 3}
Difference method with no arguments –
Example –
my_set={'this','is','edSlash'}
print("Original set:",my_set)
resultant_set=my_set.difference()
print("Resultant set:",resultant_set)
# Output -
# Original set: {'is', 'this', 'edSlash'}
# Resultant set: {'is', 'this', 'edSlash'}
Difference method with other type of collections or iterable element –
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.difference(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: {'a', 'f', 'g', 'b', 'h', 'c'}
# list: ['a', 'b', 'c', 'd', 'e']
# tuple: ('a', 'b', 'c')
# dictionary: {'a': 12, 'b': 15}
# string: ab
# Resultant set: {'f', 'g', 'h'}
Example –
A={10,11,12,13,14,15}
B={16,17,18,19,20,21}
print("Original set A:",A)
print("Original set B:",B)
C=B.difference(A)
print("Resultant Set B-A:",C)
# Output -
# Original set A: {10, 11, 12, 13, 14, 15}
# Original set B: {16, 17, 18, 19, 20, 21}
# Resultant Set B-A: {16, 17, 18, 19, 20, 21}
Difference using -
operator –
Example –
set1={'a','b','c','d','e'}
set2={'b','d','e','f','g'}
resultant_set=set1-set2
print("Original set 1:",set1)
print("Original set 2:",set2)
print("Resultant set:",resultant_set)
# Output -
# Original set 1: {'e', 'c', 'd', 'a', 'b'}
# Original set 2: {'f', 'g', 'd', 'e', 'b'}
# Resultant set: {'a', 'c'}
Office:- 660, Sector 14A, Vasundhara, Ghaziabad, Uttar Pradesh - 201012, India