Home > database >  Command raised an exception: TypeError: unhashable type: 'dict'
Command raised an exception: TypeError: unhashable type: 'dict'

Time:10-29

I want to get a line of data from a dictionary and if that line says "null" skip past it and continue the loop. When I try to do so, I get this error:

Command raised an exception: TypeError: unhashable type: 'dict'

Here is my code:

        card = requests.get('https://api.scryfall.com/cards/named?fuzzy='   search)
        prints = requests.get(card.json()['prints_search_uri'])

        for i in prints.json()['data']:
            if prints.json()[i]['prices']['usd'] == 'null':
                continue
            else:
                print(i['set_name']   ': '   i['prices']['usd'])

I am quite new to Python, and coding in general, and I haven't found an answer that I understand. How do I get rid of this error?

CodePudding user response:

I believe your problem is here:

for i in prints.json()['data']:
    if prints.json()[i]['prices']['usd'] == 'null':

The variable "i" is a dict, as can be inferred from the other lines of code where you access some of its values by their keys. Just replace prints.json()[i] with i:

for i in prints.json()['data']:
    if i['prices']['usd'] == 'null':

CodePudding user response:

In general the error means that the __hash__ method does not exist on the key type for the dict() in question.

For example:

  class Hashable:
      def __hash__(self): 
       return self.some_string

The Hashable type can be used as a key in a dictionary.

A dict() will always call __hash__ for the underlying key.

Now for your case, it seems weird that you are iterating over a list from prints.json()["data"] storing that in i and then using that to index into prints.json() it seems unrelated...

In any case I think that is where your error is, because it is evident that you i is a dict type and you are using that as a key in prints.json() which is also a dict which is an error as explained above.

  • Related