परिचय
जब डिक्शनरी की सूची से किसी डिक्शनरी को हटाना हो जब उसमें कोई विशेष मान मौजूद न हो, तो सरल पुनरावृत्ति, 'del' ऑपरेटर और 'break' स्टेटमेंट का उपयोग किया जाता है।
उदाहरण
my_list = [{"code": 1, "fun": "learn"}, {"code": 2, "fun": "code"}, {"code": 3, "fun": "test"}, {"code": 4, "fun": "teach"}, {"code": 4, "fun": "make"}, {"code": 4, "fun": "object"}]
print("The list of dictionary is : ")
print(my_list)
for index in range(len(my_list)):
if my_list[index]['code'] == 3:
del my_list[index]
break
print("The resultant list is : ")
print(my_list)
आउटपुट
The list of dictionary is : [{'code': 1, 'fun': 'learn'}, {'code': 2, 'fun': 'code'}, {'code': 3, 'fun': 'test'}, {'code': 4, 'fun': 'teach'}, {'code': 4, 'fun': 'make'}, {'code': 4, 'fun': 'object'}]
The resultant list is : [{'code': 1, 'fun': 'learn'}, {'code': 2, 'fun': 'code'}, {'code': 4, 'fun': 'teach'}, {'code': 4, 'fun': 'make'}, {'code': 4, 'fun': 'object'}]
व्याख्या
- डिक्शनरी की एक सूची परिभाषित की जाती है और कंसोल पर प्रदर्शित की जाती है।
- सूची पर पुनरावृत्ति की जाती है, और विशेष इंडेक्स की जांच की जाती है कि यह एक पूर्णांक के बराबर है या नहीं।
- इसे परिणाम में निर्दिष्ट किया जाता है।
- यह कंसोल पर आउटपुट के रूप में प्रदर्शित होता है।