1

From user input, I can make a list. It is the intention for this list to be cross-checked with the dictionary and the corresponding value to be printed in another list.

Upon inputting "food" twice, a type-error of unhashable type: "list"

What can I do to solve this problem? Either through a change in ordering or the use of novel functions? This is my code:

grocery_market = {"food": 2}

shopping_list = []

max_shop_list = 2

while len(shopping_list) < max_shop_list:
    
    item = input("Add an item: ")

    shopping_list.append(item)

    print(shopping_list)

print(grocery_market[shopping_list])
Mark
  • 90,562
  • 7
  • 108
  • 148
  • 1
    you can't pass list as key for dictionary, you have to take an element from list, such as list[ i ] e pass it to dictionary, like dictionary[list[I]]. Otherwise you can pass same item took from input to the dictionary – federikowsky Jul 16 '22 at 19:24
  • 1
    Does this answer your question? [How to get multiple dictionary values?](https://stackoverflow.com/questions/24204087/how-to-get-multiple-dictionary-values) – SuperStormer Jul 16 '22 at 19:25
  • 1
    You use looping, just like above. `for item in shopping_list:` / `print(grocery_market[item])`. – Tim Roberts Jul 16 '22 at 19:26

3 Answers3

1

I think this is what you are looking for:

grocery_market = {"food": 2}

shopping_list = []

max_shop_list = 2

while len(shopping_list) < max_shop_list:
    
    item = input("Add an item: ")

    shopping_list.append(item)

    print(shopping_list)

both = [] # Create a dictionary to put our results in

for item in shopping_list:
    if item in list(grocery_market.keys()): # Check if the item exists in the grocery market
        both.append(grocery_market[item]) # Add the item to the dictionary if found in both

print(both) # Print out the results

If you want a dictionary do

both = {}

for item in shopping_list:
    if item in list(grocery_market.keys()): # Check if the item exists in the grocery market
        both[item] = grocery_market[item] # Add the item to the dictionary if found in both
print(both)

(this was the original answer)

Jaffa
  • 189
  • 1
  • 6
  • 1
    Not quite what I wanted, but this solves a problem I have in another project so thanks. What I wanted was it to output a list of prices from the items I select i.e. input "food" in twice means an output of [2,2] somehow. – The Fearsome Asian Jul 16 '22 at 19:51
  • 1
    I've updated my answer to do that – Jaffa Jul 16 '22 at 19:58
1

Im not sure what "printed in another list" means.

But you can use lost comprehension with if statement:

print([item for item in shopping_list if item in grocery_market])

Or dict comprehension:

print({item: value for item, value in grocery_market.items() if item in shopping_list}) 
Alon Gouldman
  • 3,025
  • 26
  • 29
  • 1
    What I wanted was it to output a list of prices from the items I select i.e. input "food" in twice means an output of [2,2] somehow. – The Fearsome Asian Jul 16 '22 at 19:51
  • 1
    you can use: `[grocery_market[item] for item in shopping_list]`. this would output 2 twice, in case you have the same food in the shopping list twice – Alon Gouldman Jul 18 '22 at 07:23
1

Seems like you want to look up the value of each item in the list. Using shopping_list_values = [grocery_market[item] for item in shopping_list] will get you that.

grocery_market = {"food": 2}

shopping_list = []

max_shop_list = 2

while len(shopping_list) < max_shop_list:
    
    item = input("Add an item: ")

    shopping_list.append(item)

    print(shopping_list)

print([grocery_market[itm] for itm in shopping_list])

This prints

Add an item: 
food
['food']
Add an item: 
food
['food', 'food']
[2, 2]
ogdenkev
  • 2,264
  • 1
  • 10
  • 19