Python Update set In Hindi

📔 : Python 🔗

तो जैसा आपने पढ़ा कि Python में set unchangeable होते हैं means उन्हें update / edit या delete नहीं कर सकते हैं। अगर आप ऐसा करने की कोशिश करते भी हैं तो error आ जाती है।
For Example -

s = {12,34,56,78}
s[0] = 90

It will raise an error.
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
    s[0] = 90
TypeError: 'set' object does not support item assignment.

Python में set items को आप direct update नहीं कर सकते हैं, लेकिन set को आप list में convert करके update कर सकते हैं। list() function की help से आपको पहले set को list में convert करना पड़ेगा then update करने के बाद फिर से set() function की help से set में convert करना पड़ेगा।

Python Update set Example

Copy Fullscreen Close Fullscreen Run
s = {'Aryendra', 34, 'Agra', 'India'}
print('Before update :', s)
#convert into list.
l = list(s)

#now we can update items.
l[0] = 'Aryendra Sharma'
l[1] = 24

#again convert into set.
s = set(l)
print('After update', s)
Output
C:\Users\Rahulkumar\Desktop\python>python set_update.py
Before update : {34, 'Agra', 'India', 'Aryendra'}
After update {24, 'Aryendra Sharma', 'India', 'Aryendra'}

किसी एक type के data को दुसरे type में convert करने को ही Type Casting कहते हैं।

Python Adding New Item In set

set में हम items को update तो नहीं कर सकते लेकिन new item को add() method के help से directly add कर सकते हैं।

Copy Fullscreen Close Fullscreen Run
s = {'Aryendra', 'Rahul'}
print('Before append:', s)

#we can directly add new item in to set.
s.add('Mohit')
s.add('Gyan Singh')
s.add('Girish')
print('After append :', s)
Output
C:\Users\Rahulkumar\Desktop\python>python set_add.py
Before append: ('Aryendra', 'Rahul')
After append : {'Gyan Singh', 'Aryendra', 'Rahul', 'Girish', 'Mohit'}

Python Delete set Item

items को set से delete करने के लिए आप pop(), remove() या discard() method का use कर सकते हैं।

Copy Fullscreen Close Fullscreen Run
s = {12,2,45,67,89,90}
#delete last element.
s.pop()
print('After deleting last element :', s)

#delete specified item.
s.discard(12)
print(s)
Output
C:\Users\Rahulkumar\Desktop\python>python set_delete.py
After deleting last element : {67, 12, 45, 89, 90}
{67, 45, 89, 90}

Note : remove() और discard() दोनों ही methods का use करके किसी particular item को delete कर सकते हैं , difference सिर्फ इतना है कि remove() method में pass को गयी value न मिली तो error generate हो जयगी जबकि remove() method कोई error generate नहीं करता है।


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 delete set

del keyword का use करके हम किसी भी set को completely delete कर सकते हैं।

s = {12,4,546,5657}
del s
print(s) 
#It generates an error 

Traceback (most recent call last):
  File "<stdin>", line 3, in <module>
    print(s)
NameError: name 's' is not defined 

because now tuple doesn't exist.

अब आप समझ गएँ होंगे कि Python में set कितने different हैं , list और tuple से।

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