जब यह जांचना हो कि 'K' की के लिए कोई विशेष मान मौजूद है या नहीं, तो लिस्ट कॉम्प्रिहेंशन का उपयोग किया जाता है।
उदाहरण
my_list = [{"python" : "14", "is" : "great", "fun" : "1`"},{"python" : "cool", "is" : "fun", "best" : "81"},{"python" : "93", "is" : "CS", "amazing" : "16"}]
print("The list is :")
print(my_list)
K = "python"
print("The value of K is ")
print(K)
value = "cool"
my_result = value in [index[K] for index in my_list]
print("The result is :")
if(my_result == True):
print("The value is present in with respect to key ")
else:
print("The value isn't present with respect to key")आउटपुट
The list is : [{'python': '14', 'is': 'great', 'fun': '1`'}, {'python': 'cool', 'is': 'fun', 'best': '81'}, {'python': '93', 'is': 'CS', 'amazing': '16'}]
The value of K is python
The result is : The value is present in with respect to keyव्याख्या
डिक्शनरी एलिमेंट्स की एक लिस्ट परिभाषित की गई है और कंसोल पर प्रदर्शित की गई है।
K का मान परिभाषित किया गया है और कंसोल पर प्रदर्शित किया गया है।
एक और स्ट्रिंग परिभाषित की गई है।
लिस्ट कॉम्प्रिहेंशन का उपयोग करके लिस्ट पर पुनरावृति की जाती है और डिक्शनरी की लिस्ट में K वैल्यू के इंडेक्स को खोजा जाता है।
इसे एक वेरिएबल में असाइन किया जाता है।
इस वेरिएबल के 'True' या 'False' होने के आधार पर, प्रासंगिक संदेश कंसोल पर प्रदर्शित होता है।