I'm a beginner at Python, so there might be something strange about it.I would appreciate it if you could point it out.
A(list) = ['Fish', 'Tori', 'Dog', 'Cat', 'Rabbit']
B (dictionary) = {'Fish': 'Tuna', 'Katsuo', 'Dog': 'Shiba-nu', 'Pomeranian'}
(I wrote a simple list and dictionary for explanation, but there are actually more.)
If there is a list and dictionary like the one above, I would like to see the key and the value if the element A exists in the key B.
I would appreciate it if the results were as follows.
'Fish': 'Tuna', 'Katsuo'
'Dog': 'A little dog', 'Pomeranian'
B
is a value similar to the following (key is a string and value is listed):
A=['Fish', 'Tori', 'Dog', 'Cat', 'Rabbit']
B = {'Fish': ['Tuna', 'Katsuo', 'Dog': ['Shiba'', 'Pomeranian']}
matched = {a:B[a] for a if a in B}
print(matched)
# {'Fish':['Maguro', 'Katsuo', 'Dog':['Shiba Dog', 'Pomeranian']}
© 2025 OneMinuteCode. All rights reserved.