If tutorials available on this website are helpful for you, please whitelist this website in your ad blocker😭 or Donate to help us ❤️ pay for the web hosting to keep the website running.
Python में dictionary के कई predefined methods कुछ तरह हैं -
Python में किसी particular dictionary items को access करने के लिए get() method का use भी कर सकते हैं।
d = {
'name' : 'Rahul Kumar Rajput',
'age' : 25,
'city' : 'Agra',
'country' : 'India'
}
#access name.
print('Name :', d.get('name'))
#access city.
print('City :', d.get('city'))
C:\Users\Rahulkumar\Desktop\python>python dict_get.py Name : Rahul Kumar Rajput City : Agra
किसी dictionary की सभी keys extract करने के लिए keys() method का use किया जाता है। keys() method सभी keys की list return करता है।
d = {
'name' : 'Rahul Kumar Rajput',
'age' : 25,
'city' : 'Agra',
'country' : 'India'
}
print(d.keys())
C:\Users\Rahulkumar\Desktop\python>python dict_keys.py dict_keys(['name', 'age', 'city', 'country'])
किसी dictionary की सभी values extract करने के लिए values() method का use किया जाता है। values() method सभी dictionary values की list return करता है।
d = {
'name' : 'Rahul Kumar Rajput',
'age' : 25,
'city' : 'Agra',
'country' : 'India'
}
print(d.values())
C:\Users\Rahulkumar\Desktop\python>python dict_values.py dict_values(['Rahul Kumar Rajput', 25, 'Agra', 'India'])
items() method, dictionary में present हर key : value pair का एक tuple बनाकर एक list return है। मतलब items() method tuples की list return करता है, उस list में मौजूद हर एक tupleमें दो item होते हैं - पहला key , दूसरा उस key से associate value.
d = {
'name' : 'Rahul Kumar Rajput',
'age' : 25,
'city' : 'Agra',
'country' : 'India'
}
print(d.items())
C:\Users\Rahulkumar\Desktop\python>python dict_items.py dict_items([('name', 'Rahul Kumar Rajput'), ('age', 25), ('city', 'Agra'), ('country', 'India')])
dictionary का update() method new item add और existing item को update करने के लिए use कर सकते हैं। जो dictionary को as a parameter accept करता है , जिसमे हम updated values को key के साथ रखते हैं। अगर key exist है तो value update हो जायगी और अगर new हुई तो नया item add हो जायगा।
d = {
'name' : 'Rahul',
}
print('Before update :', d)
#update name.
d.update({'name':'Rahul Kumar Rajput'})
#add new item : country.
d.update({'country':'India'})
print('After update :', d)
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'}
pop() method का use करके आप किसी specified item को delete कर सकते हैं , यह method एक parameter as a value accept करता है जिसे हमें delete करना है।
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)
C:\Users\Rahulkumar\Desktop\python>python dict_pop.py Before delete : {'name': 'Rahul', 'age': 25, 'country': 'India', 'other': 'Other Things'} After delete : {'name': 'Rahul', 'country': 'India'}
poppopitem) method का use dictionary से last item को delete करने के लिए use किया जाता है , यह भी एक parameter as a value accept करता है जिसे हमें delete करना है।
d = {
'name' : 'Rahul',
'age' : 25,
'country' : 'India',
}
print('Before delete :', d)
#delete country.
d.popitem()
#delete other.
d.popitem()
print('After delete :', d)
C:\Users\Rahulkumar\Desktop\python>python dict_popitem.py Before delete : {'name': 'Rahul', 'age': 25, 'country': 'India'} After delete : {'name': 'Rahul'}
clear() method का use dictionary को completely empty करने के लिए किया जाता है।
See Example -
dict_var.clear()
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