Python : Set Methods

MethodDescription
add()set के अंदर नया element add करना।
clear()set के सारे element को remove करता है।
copy()set की copy को return करता है।
difference()दो या दो से अधिक सेटों के बीच के difference वाले set को return करता है।
difference_update()set से उस item को रिमूव करता है जो item दोनों set में हो और साथ ही उस सेट को update करता है।
discard()यह method सेट से किसी specific item को remove करता है।
intersection()दो सेट में स्टोर एक जैसे आइटम को return करता है।
intersection_update()intersection_update() method उन item को रिमूव करती है जो आइटम दोनों sets में मौजूद नहीं हैं। और set को update करता है।
isdisjoint()यदि दोनों सेटों में से एक जैसा कोई भी item मौजूद नहीं है, तो isdisjoint () method true return करता है, अन्यथा false
issubset()Returns whether another set contains this set or not
issuperset()Returns whether this set contains another set or not
pop()Removes an element from the set
remove()Removes the specified element
symmetric_difference()Returns a set with the symmetric differences of two sets
symmetric_difference_update()inserts the symmetric differences from this set and another
union()यह एक नया set return करता है जिसमे original सेट के सारे item होते है।
update()Update the set with the union of this set and others

add( ) method

set के अंदर नया element add करना।

veg= {"carrot","corn","onion","potato"}
veg.add("pumpkin")
print(veg)
OUTPUT
{'pumpkin', 'onion', 'potato', 'carrot', 'corn'}

clear() method

set के सारे element को remove करता है।

veg= {"carrot","corn","onion","potato"}
veg.clear()
print(veg)
OUTPUT
set()

copy() method

set की copy को return करता है।

veg= {"carrot","corn","onion","potato"}
x=veg.copy()
print(x)
OUTPUT
{'carrot', 'corn', 'potato', 'onion'}

difference() method

दो या दो से अधिक सेटों के बीच के difference वाले set को return करता है।

x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
z = x.difference(y)
print(z)
OUTPUT
{'cherry', 'banana'}

difference_update() method

set से उस item को रिमूव करता है जो item दोनों set में हो और साथ ही सेट को update करता है।

x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
x.difference_update(y)
print(x)
OUTPUT
{'cherry', 'banana'}

discard() method

यह method सेट से किसी specific item को remove करता है।

veg= {"carrot","corn","onion","potato"}
veg.discard("corn")
print(veg)
OUTPUT
{'carrot', 'potato', 'onion'}

intersection() method

दो सेट में स्टोर एक जैसे आइटम को return करता है।

veg1= {"carrot","corn","onion","potato"}
veg2={"carrot","beetroot","tomato"}
veg = veg1.intersection(veg2)
print(veg)
OUTPUT
{'carrot'}

intersection_update() method

intersection_update() method उन item को रिमूव करती है जो आइटम दोनों sets में मौजूद नहीं हैं। और set को update करता है।

veg1= {"carrot","corn","onion","potato"}
veg2={"carrot","beetroot","tomato"}
veg1.intersection_update(veg2)
print(veg1)
OUTPUT
{'carrot'}

Note : intersection_update() मेथड intersection() मेथड से अलग है। क्योकि intersection() method बिना unwanted item को रिमूव किये बिना एक नया set return करता है। जबकि intersection_update() मेथड original set से unwanted item को रिमूव करता है।

 isdisjoint() Method

यदि दोनों सेटों में से एक जैसा कोई भी item मौजूद नहीं है, तो isdisjoint () method true return करता है, अन्यथा false

x={5,8,9,6,3}
y={11,12,16,15}
z=x.isdisjoint(y)
print(z)
OUTPUT
True
-----------------------------------------------
a={2,8,9,6}
b={2,9,5,6}
c=a.isdisjoint(b)
print(c)
OUTPUT
False

union() Method

यह एक नया set return करता है जिसमे original सेट और specific सेट के सारे item होते है। लेकिन ये duplicate वैल्यू को repeat नहीं करता।

A={1,2,3,4}
B={3,5,6}
AUB = A.union(B)
print(AUB)
OUTPUT
{1, 2, 3, 4, 5, 6}