उदाहरण
नीचे इसका प्रदर्शन दिया गया है:
my_list = [{"Python": 2, "is": 4, "cool": 11}, {"Python": 5, "is": 1, "cool": 1}, {"Python": 7, "is": 3, "cool": 7}, {"Python": 9, "is": 9, "cool": 8}, {"Python": 4, "is": 10, "cool": 6}]
print("The list is :")
print(my_list)
search_list = [1, 9, 8, 4, 5]
key = "is"
my_result = []
for sub in my_list:
if sub[key] in search_list:
my_result.append(sub)
print("The result is :")
print(my_result)
आउटपुट
The list is : [{'Python': 2, 'is': 4, 'cool': 11}, {'Python': 5, 'is': 1, 'cool': 1}, {'Python': 7, 'is': 3, 'cool': 7}, {'Python': 9, 'is': 9, 'cool': 8}, {'Python': 4, 'is': 10, 'cool': 6}]
The result is : [{'Python': 2, 'is': 4, 'cool': 11}, {'Python': 5, 'is': 1, 'cool': 1}, {'Python': 9, 'is': 9, 'cool': 8}]
व्याख्या
डिक्शनरी की एक सूची परिभाषित की गई है और कंसोल पर प्रदर्शित की गई है।
पूर्णांकों की एक और सूची और एक कुंजी परिभाषित की गई है।
एक खाली सूची परिभाषित की गई है।
सूची पर पुनरावृति की जाती है और यदि कुंजी मिलती है, तो तत्व को खाली सूची में जोड़ा जाता है।
यह आउटपुट है।
यह कंसोल पर प्रदर्शित होता है।