Reputation: 29
So I stumbled over another problem that I just can't seem to get.
I have this dictionary
fruit_count = {'apple': 5, 'banana': 7, 'orange': 4, 'melon': 9}
and
and a nested dictionary
students_fruit_count = { 'Brandon': {'apple': 2, 'banana': 7, 'orange': 3, 'melon': 8},
'Joan': {'apple': 11, 'banana': 8, 'orange': 3, 'melon': 6},
'Tanya': {'apple': 5, 'banana': 7, 'orange': 4, 'melon': 9}}
Now I am trying to build a code that prints out the name of the student that has the exact same fruit counts as the fruit_count dictionary values.
So in the problem above, the code should print out Tanya
.
Here's my code (which obviously doesn't work):
for key in fruit_dict:
for key2 in students_fruit_count:
if key[i] == key2[j]:
print('key2')
Any advice or explanation to such problem? Thank you.
Upvotes: 0
Views: 62
Reputation: 1098
for i in students_fruit_count:
if students_fruit_count[i] == fruit_count:
print(i)
Or for a more Pythonic approach
result = [i for i in students_fruit_count if students_fruit_count[i]==fruit_count]
This will return a list containing all student names.
Upvotes: 1