Python Update dictionary In Hindi

📔 : Python 🔗

चूंकि dictionary में हम key : value pair में data store करते हैं , इसलिए हम key के bases पर easily data update कर सकते हैं। जो भी आपको update करनी है बस वो value assign कर देनी है।

Python dictionary Update Example

Copy Fullscreen Close Fullscreen Run
d = {
  'name' : 'Rahul',
  'age' : 45,
  'country' : 'India'
}
print('Before update :', d)
#update name.
d['name'] = 'Rahul Kumar Rajput'

#update age.
d['age'] = 24
print('After update :', d)
Output
C:\Users\Rahulkumar\Desktop\python>python dict_update.py
Before update : {'name': 'Rahul', 'age': 45, 'country': 'India'}
After update : {'name': 'Rahul Kumar Rajput', 'age': 24, 'country': 'India'}

Python Adding New Item In Dictionary

dictionary में new item add करने के लिए आपको new key को bas एक value assign कर देना है, जैसे update करते थे बैसे ही। update करने के लिए existing key को new value को assign करते हैं। जबकि new item के लिए new key : value रखते हैं।
See Example -

Copy Fullscreen Close Fullscreen Run
d = {'name' : 'Rahul'}
print('Before add :', d)

#add new item: age, country.
d['age'] = 24
d['country'] = 'India'
print('After add :', d)
Output
C:\Users\Rahulkumar\Desktop\python>python dict_add.py
Before add : {'name': 'Rahul'}
After add : {'name': 'Rahul', 'age': 24, 'country': 'India'}

Python dictionary update()

इसके अलावा dictionary का predefined method update() भी new item add और existing item को update करने के लिए use कर सकते हैं। जो dictionary को as a parameter accept करता है , जिसमे हम updated values को ही key के साथ रखते हैं। अगर key exist है तो value update हो जायगी और अगर new हुई तो नया item add हो जायगा।

Copy Fullscreen Close Fullscreen Run
d = {
  'name' : 'Rahul',
  'age' : 25,
}
print('Before update :', d)
#update name.
d.update({'name':'Rahul Kumar Rajput'})

#add new item : country.
d.update({'country':'India'})
print('After update :', d)
Output
C:\Users\Rahulkumar\Desktop\python>python dict_update.py
Before update : {'name': 'Rahul', 'age': 25}
After update : {'name': 'Rahul Kumar Rajput', 'age': 25, 'country': 'India'}

Python Delete Dictionary Item

pop() method का use करके आप किसी specified item को delete कर सकते हैं , यह method एक parameter as a value accept करता है जिसे हमें delete करना है।
Example -

Copy Fullscreen Close Fullscreen Run
d = {
  'name' : 'Rahul',
  'age' : 25,
  'country' : 'India',
  'other' : 'Other Things'
}
print('Before delete :', d)

#delete age.
d.pop('age')

#delete other.
d.pop('other')

print('After delete :', d)
Output
C:\Users\Rahulkumar\Desktop\python>python dict_update.py
Before delete : {'name': 'Rahul', 'age': 25, 'country': 'India', 'other': 'Other Things'}
After delete : {'name': 'Rahul', 'country': 'India'}

Note : अगर pass की गयी key नहीं मिली तो error generate होगी- "KeyError: 'keyName'".


Python popitem()

popitem() का use dictionary से last item को delete करने के लिए use किया जाता है , यह भी एक parameter as a value accept करता है जिसे हमें delete करना है

dict_var.popitem()

Note :popitem() method use करने के लिए dictionary empty नहीं होनी चाहिए , नहीं तो KeyError: 'popitem(): dictionary is empty' error generate होगी।


Python del

del keyword , किसी particular item या complete dictionary को delete करता है।
See Example -

Copy Fullscreen Close Fullscreen Run
d = {
  'name' : 'Rahul',
  'age' : 25,
  'other' : 'Other Things'
}
print('Before delete :', d)

#delete other.
del d['other']
print('After delete :', d)

#now delete complete dictionary : d.
del d
Output
C:\Users\Rahulkumar\Desktop\python>python dict_update.py
Before delete : {'name': 'Rahul', 'age': 25, 'other': 'Other Things'}
After delete : {'name': 'Rahul', 'age': 25}

I Hope, अब आप समझ गए होंगे कि Python में dictionary items को कैसे update या delete करते हैं।

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