Reputation: 317
I have two dictionaries: (1)Inventory and (2)Items
Under these dictionaries are tuples which are added using user's input.
dict_inventory = {('fruits', ['apple','mango'])
dict_items = {('apple', [3, 5])
('mango', [4, 6])}
How do I compare the two and to match similar values which are apple
and mango
My code is not printing anything:
for itemA in dict_inventory.values():
for itemB in dict_items.keys():
if itemA == itemB:
print("Match!")
Upvotes: 1
Views: 843
Reputation: 4023
Your original for-loop
was getting the values from the inventory dictionary as a list
rather than a string
when you iterated through the values. Since it returned a list, you will need to iterate through those values as well. This should get you running:
inventory = {
"fruits": ["apples", "mangos",]
}
items = {
"apples": [3, 5],
"mangos": [4, 6],
}
for value in inventory.values():
for item_a in value:
if item_a in items.keys():
print("Match!")
However, you could just merge the two dictionaries.
inventory = {
"fruits": {
"apples": [3, 5],
"mangos": [4, 6],
}
}
Upvotes: 1