Python में dictionary के लिए कई built-in methods उपलब्ध हैं जिनका इस्तेमाल data manipulation के लिए किया जाता है। नीचे इनके बारें में दिया गया है:-
1:- clear()
clear() method का उपयोग dictionary के सारे items को remove करने के लिए किया जाता है। इसका मतलब है कि dictionary खाली हो जाएगी, लेकिन dictionary की structure बनी रहती है।
Example:-
my_dict = {'name': 'John', 'age': 25}
my_dict.clear()
print(my_dict) # Output: {}
2:- copy()
copy() मेथड का उपयोग dictionary का एक नया copy बनाने के लिए किया जाता है। इसका मतलब है कि original dictionary की एक जैसी copy बनाई जाती है, लेकिन यह एक अलग object होता है। यानी अगर हम इस copy में कुछ बदलाव करते हैं, तो original dictionary पर कोई असर नहीं पड़ता।
Example-
my_dict = {'name': 'John', 'age': 25}
new_dict = my_dict.copy()
print(new_dict) # Output: {'name': 'John', 'age': 25}
3:- fromkeys()
fromkeys() method का उपयोग नयी dictionary बनाने के लिए किया जाता है, जिसमें आपको keys और default value देने का option मिलता है। अगर default value नहीं दी जाती है, तो value None पर set होती है।
Example-
keys = ['name', 'age']
value = 0
new_dict = dict.fromkeys(keys, value)
print(new_dict) # Output: {'name': 0, 'age': 0}
4:- get()
get() मेथड का उपयोग dictionary से किसी key की value प्राप्त (fetch) करने के लिए किया जाता है। अगर वह key डिक्शनरी में मौजूद नहीं होती, तो यह None return करता है। आप चाहें तो कोई default value भी दे सकते हैं, जो key ना मिलने पर return होगी।
Example-
my_dict = {'name': 'John', 'age': 25}
print(my_dict.get('name')) # Output: John
print(my_dict.get('address', 'Not Found')) # Output: Not Found
5:- items()
items() मेथड का उपयोग dictionary के सभी key-value pairs को tuple के रूप में return करने के लिए किया जाता है। यह method हमें dictionary के अंदर के items को आसानी से iterate करने की सुविधा देता है।
Example-
my_dict = {'name': 'John', 'age': 25}
for key, value in my_dict.items():
print(key, value)
# Output:
# name John
# age 25
6:- keys()
keys() मेथड dictionary की सभी keys को return करता है। यह हमें keys का एक view object देता है, जिससे हम सभी keys पर loop चला सकते हैं और उन्हें आसानी से access कर सकते हैं।
Example:-
my_dict = {'name': 'John', 'age': 25}
print(my_dict.keys()) # Output: dict_keys(['name', 'age'])
7:- pop()
pop() method का उपयोग dictionary से किसी विशेष key को हटाने के लिए किया जाता है और उस key की value को return करता है। यदि वह key डिक्शनरी में नहीं है, तो error आता है। आप इसमें एक default value भी दे सकते हैं।
Example:-
my_dict = {'name': 'John', 'age': 25}
removed_value = my_dict.pop('age')
print(removed_value) # Output: 25
print(my_dict) # Output: {'name': 'John'}
8:- popitem()
popitem() मेथड dictionary से आखिरी key-value pair को remove करता है और उसे return करता है। यह method तब उपयोगी होता है जब आप dictionary को update करना चाहते हैं या last item को remove करना चाहते हैं।
Example:-
my_dict = {'name': 'John', 'age': 25}
last_item = my_dict.popitem()
print(last_item) # Output: ('age', 25)
print(my_dict) # Output: {'name': 'John'}
9:- setdefault()
setdefault() method का उपयोग dictionary में किसी key की value को check करने के लिए किया जाता है। अगर key मौजूद नहीं है, तो यह key को default value के साथ dictionary में add कर देता है।
my_dict = {'name': 'John'}
my_dict.setdefault('age', 25)
print(my_dict) # Output: {'name': 'John', 'age': 25}
इसे पढ़ें:-
Summary (सारांश)
Python dictionary methods पर यह Hindi article बहुत ही सरल भाषा में समझाता है कि कैसे आप dictionary के साथ data manipulation कर सकते हैं। इसमें निम्नलिखित methods को उदाहरण सहित दिया गया है:
- clear() – dictionary के सभी items को हटाकर उसे खाली कर देता है, लेकिन उसकी structure बनी रहती है।
- copy() – original dictionary की एक नई copy बनाता है, जो एक अलग object होता है। इसमें बदलाव करने पर original पर कोई effect नहीं पड़ता।
- fromkeys() – दी गई keys और एक default value से नई dictionary बनाता है। अगर value न दें तो None set होता है।
- get() – किसी key की value fetch करता है। अगर key मौजूद नहीं हो तो None या आपकी दी हुई default value return करता है।
- items() – सभी key-value pairs को tuples के रूप में return करता है, जो iteration के लिए उपयोगी है।