पायथन - शब्दकोश आइटम निकालें
आइटम हटाना
शब्दकोश से आइटम निकालने की कई विधियाँ हैं:
उदाहरण
विधि निर्दिष्ट कुंजी नाम वाले pop()
आइटम को हटा देती है:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict.pop("model")
print(thisdict)
उदाहरण
विधि अंतिम सम्मिलित आइटम को popitem()
हटा देती है (3.7 से पहले के संस्करणों में, इसके बजाय एक यादृच्छिक आइटम हटा दिया जाता है):
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict.popitem()
print(thisdict)
उदाहरण
कीवर्ड निर्दिष्ट कुंजी नाम वाले del
आइटम को हटा देता है:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
del thisdict["model"]
print(thisdict)
उदाहरण
del
कीवर्ड डिक्शनरी को पूरी तरह से मिटा भी सकता है :
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
del thisdict
print(thisdict) #this will cause an error because "thisdict"
no longer exists.
उदाहरण
विधि शब्दकोश को clear()
खाली करती है:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict.clear()
print(thisdict)