Python Dictionary Methods
Introduction
Python dictionaries come with many built-in methods that allow you to manipulate, access, and modify the dictionary data effectively. This tutorial explains the most commonly used dictionary methods with clear examples.
Python dictionary में कई built-in methods होते हैं जो dictionary डेटा को प्रभावी ढंग से access, modify और manipulate करने में मदद करते हैं। इस ट्यूटोरियल में हम सबसे अधिक उपयोग होने वाले dictionary methods को उदाहरणों के साथ समझेंगे।
Common Python Dictionary Methods
update()- Update dictionary with key-value pairs from another dictionary or iterable.pop()- Remove and return the value for a specified key.popitem()- Remove and return the last inserted key-value pair.clear()- Remove all items from the dictionary.copy()- Return a shallow copy of the dictionary.setdefault()- Returns value if key exists, else inserts key with specified default value.fromkeys()- Create a new dictionary from given keys with a default value.
update()- दूसरी dictionary या iterable से key-value जोड़ें या अपडेट करें।pop()- दिए गए key का value निकालें और dictionary से हटाएं।popitem()- आखिरी जोड़ा गया key-value pair निकालें।clear()- dictionary के सभी items हटाएं।copy()- dictionary की shallow copy बनाएं।setdefault()- अगर key है तो उसका value दें, नहीं तो default value के साथ key जोड़ें।fromkeys()- दिए गए keys से एक नई dictionary बनाएं, सभी keys की default value समान हो।
Example 1: update() Method
my_dict = {'name': 'Amit', 'age': 20}
my_dict.update({'age': 21, 'city': 'Delhi'})
print(my_dict)
Update the dictionary by changing 'age' and adding 'city'.
dictionary में 'age' बदलें और 'city' जोड़ें।
Output:
Example 2: pop() Method
my_dict = {'name': 'Amit', 'age': 20}
age = my_dict.pop('age')
print(age)
print(my_dict)
Remove 'age' key and return its value.
'age' key हटाएं और उसकी value वापस पाएं।
Output:
{'name': 'Amit'}
Example 3: popitem() Method
my_dict = {'name': 'Amit', 'age': 20}
pair = my_dict.popitem()
print(pair)
print(my_dict)
Remove and return the last inserted key-value pair.
आखिरी जोड़ा गया key-value pair हटाएं और दिखाएं।
Output:
{'name': 'Amit'}
Example 4: clear() Method
my_dict = {'name': 'Amit', 'age': 20}
my_dict.clear()
print(my_dict)
Remove all items, resulting in an empty dictionary.
सभी items हटाएं, खाली dictionary बनाएं।
Output:
Example 5: copy() Method
my_dict = {'name': 'Amit', 'age': 20}
new_dict = my_dict.copy()
print(new_dict)
Create a shallow copy of the dictionary.
dictionary की shallow copy बनाएं।
Output:
Example 6: setdefault() Method
my_dict = {'name': 'Amit'}
age = my_dict.setdefault('age', 25)
print(age)
print(my_dict)
Returns value of 'age' if present, otherwise inserts 'age' with default 25.
'age' की value दें अगर हो, नहीं तो default 25 के साथ जोड़ें।
Output:
{'name': 'Amit', 'age': 25}
Example 7: fromkeys() Method
keys = ['a', 'b', 'c']
new_dict = dict.fromkeys(keys, 0)
print(new_dict)
Create a new dictionary with keys from list and default value 0.
keys से नई dictionary बनाएं, सभी keys की value 0 हो।
Output: