Python में set को modify करने के लिए कई useful methods हैं जिनका use करके हम set को जरूरत के हिसाब से use कर सकते हैं। Python में set methods की list कुछ इस तरह है।

  1. add()
  2. clear()
  3. copy()
  4. remove()
  5. discard()
  6. pop()
  7. union()
  8. isupperset()
  9. issubset
  10. difference
  11. intersection

Python add

यह list में new element add करता है , new element set के last में insert होता है।
For Example -

Copy Fullscreen Close Fullscreen Run
s = {'Banana', 'Papaya'}
print('Before add :', s)
s.add('Grapes')
s.add('Mango')
print('After add :', s)
Output
C:\Users\Rahulkumar\Desktop\python>python set_add.py
Before add : {'Banana', 'Papaya'}
After add : {'Grapes', 'Mango', 'Banana', 'Papaya'}

Python set clear

यह set को पूरी तरह से clear / empty करके empty set return करता है।
See Example -

Copy Fullscreen Close Fullscreen Run
s = {'Banana', 'Papaya'}
print('Before clear :', s)
s.clear()
print('After clear :', s)
Output
C:\Users\Rahulkumar\Desktop\python>python set_clear.py
Before clear : {'Banana', 'Papaya'}
After clear : set()

Python set copy

अगर हमें किसी set की independently copy बनानी है तो copy() method का use करते हैं।

अगर हम सिर्फ assign करते हैं जैसे s2 = s1 तो यह copy नहीं होता बस variable अलग हो जाते हैं set same ही रहता है , इनमे से किसी भी set में changes करने पर दोनों जगह होंगे।

See Example -

Copy Fullscreen Close Fullscreen Run
s1 = {'Banana', 'Papaya'}
s2 = s1.copy()
#add an item in s1 & s2 to see the difference.
s1.add('Mango')
s2.add('Grapes')
print('set s1 :', s1)
print('set s2 :', s2)
Output
C:\Users\Rahulkumar\Desktop\python>python set_copy.py
set s1 : {'Papaya', 'Banana', 'Mango'}
set s2 : {'Papaya', 'Banana', 'Grapes'}

Python set pop

pop() method set से last item को remove करता है।
See Example -

Copy Fullscreen Close Fullscreen Run
s= {'Banana', 'Papaya', 'Mango'}
s.pop()
print('After remove :', s)
Output
C:\Users\Rahulkumar\Desktop\python>python set_pop.py
After remove : {'Banana', 'Papaya'}

Note : set का pop() method , list के pop() method से different है। set का pop() method कोई भी argument नहीं लेता , जबकि list का pop() method item index as an argument लेता था।

Python set discard

discard() method का use किसी particular item को set से delete करने के लिए use किया जाता है। अगर pass की गयी value set में मिलती है तो value delete हो जायगी और नहीं मिलती है तो कोई बात नहीं।
Example -

Copy Fullscreen Close Fullscreen Run
s= {'Banana', 'Papaya', 'Mango', 'Apple'}
s.discard('Banana')

#now try to delete an item that is not exist in set.
s.discard('Black Berry')
print('After discard :', s)
Output
C:\Users\Rahulkumar\Desktop\python>python set_discard.py
After discard : {'Papaya', 'Mango', 'Apple'}

Python set remove

remove() method का use किसी भी particular item को set से delete करने के लिए use किया जाता है। लेकिन difference सिर्फ इतना है कि अगर pass की गयी value set में मिलती है error generate हो जायगी।
Example -

Copy Fullscreen Close Fullscreen Run
s= {'Banana', 'Papaya', 'Mango', 'Apple'}
s.remove('Banana')
print('After remove :', s)code>
Output
C:\Users\Rahulkumar\Desktop\python>python set_remove.py
After remove : {'Papaya', 'Mango', 'Apple'}

#now try to remove an item that is not exist in set.
s.remove('Black Berry')
#It generates an error 

Traceback (most recent call last):
  File "<stdin>", line 3, in <module>
    print(s)
KeyError: 'Black Berry'

because now set doesn't exist.

Python set union

अगर आप दो या दो से अधिक sets को मिलाकर उनमे से duplicate values remove करना चाहते हैं तो union() method का use करके कर सकते हैं।

Copy Fullscreen Close Fullscreen Run
s1 = {'Aryendra', 'Rahul'}
s2 = {'Mohit', 'Rahul', 'Girish'}

s1 = s1.union(s2)
print('After union :', s1)
Output
C:\Users\Rahulkumar\Desktop\python>python set_union.py
After union : {'Rahul', 'Girish', 'Aryendra', 'Mohit'}

Python issuperset

issuperset() check करता है कि कोई set किसी दुसरे set का super set है , मतलब किसी दुसरे set के सभी items पहले set से belong करते हैं। यह Boolean value True / False return करता है।
See Example -

Copy Fullscreen Close Fullscreen Run
s1 = {12,2,34,45, 56,67,78}
s2 = {12, 2}

#insert any different item that is not exist in s1.
s3 = {12,2,123, 89}
print('s1 is superset of s2 :', s1.issuperset(s2))
print('s1 is superset of s3 :', s1.issuperset(s3))
Output
C:\Users\Rahulkumar\Desktop\python>python set_issuperset.py
s1 is superset of s2 : True
s1 is superset of s3 : False

Python issubset

issubset() check करता है कि कोई set किसी दुसरे set का sub set है , मतलब किसी पहले set के सभी items दुसरे set से belong करते हैं। यह Boolean value True / False return करता है।
See Example -

Copy Fullscreen Close Fullscreen Run
s1 = {12, 2}
s2 = {12,2,34,45, 56,67,78}

#insert any different item that is not exist in s1.
s3 = {12,2,123, 89}
print('s1 is subset of s2 :', s1.issubset(s2))
print('s3 is subset of s2 :', s3.issubset(s2))
Output
C:\Users\Rahulkumar\Desktop\python>python set_issubset.py
s1 is subset of s2 : True
s3 is subset of s2 : False

Python intersection

intersection() किन्ही दो set में से common items का set return करता है , common न मिलने पर empty set return करता है।
Example -

Copy Fullscreen Close Fullscreen Run
s1 = {12,2,34,45, 56,67,78}
s2 = {12, 2, 34}

#insert items that are not exist in s1.
s3 = {100, 200,300}
print('common items from s1 and s2 :', s1.intersection(s2))
print('common items from s1 and s3 :', s1.intersection(s3))
Output
C:\Users\Rahulkumar\Desktop\python>python set_intersection.py
common items from s1 and s2 : {2, 12, 34}
common items from s1 and s3 : set()

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers