How to Overcome Typeerror: Unhashable Type: 'List'

How to overcome TypeError: unhashable type: 'list'

As indicated by the other answers, the error is to due to k = list[0:j], where your key is converted to a list. One thing you could try is reworking your code to take advantage of the split function:

# Using with ensures that the file is properly closed when you're done
with open('filename.txt', 'rb') as f:
d = {}
# Here we use readlines() to split the file into a list where each element is a line
for line in f.readlines():
# Now we split the file on `x`, since the part before the x will be
# the key and the part after the value
line = line.split('x')
# Take the line parts and strip out the spaces, assigning them to the variables
# Once you get a bit more comfortable, this works as well:
# key, value = [x.strip() for x in line]
key = line[0].strip()
value = line[1].strip()
# Now we check if the dictionary contains the key; if so, append the new value,
# and if not, make a new list that contains the current value
# (For future reference, this is a great place for a defaultdict :)
if key in d:
d[key].append(value)
else:
d[key] = [value]

print d
# {'AAA': ['111', '112'], 'AAC': ['123'], 'AAB': ['111']}

Note that if you are using Python 3.x, you'll have to make a minor adjustment to get it work properly. If you open the file with rb, you'll need to use line = line.split(b'x') (which makes sure you are splitting the byte with the proper type of string). You can also open the file using with open('filename.txt', 'rU') as f: (or even with open('filename.txt', 'r') as f:) and it should work fine.

Python, TypeError: unhashable type: 'list'

The problem is that you can't use a list as the key in a dict, since dict keys need to be immutable. Use a tuple instead.

This is a list:

[x, y]

This is a tuple:

(x, y)

Note that in most cases, the ( and ) are optional, since , is what actually defines a tuple (as long as it's not surrounded by [] or {}, or used as a function argument).

You might find the section on tuples in the Python tutorial useful:

Though tuples may seem similar to lists, they are often used in different situations and for different purposes. Tuples are immutable, and usually contain an heterogeneous sequence of elements that are accessed via unpacking (see later in this section) or indexing (or even by attribute in the case of namedtuples). Lists are mutable, and their elements are usually homogeneous and are accessed by iterating over the list.

And in the section on dictionaries:

Unlike sequences, which are indexed by a range of numbers, dictionaries are indexed by keys, which can be any immutable type; strings and numbers can always be keys. Tuples can be used as keys if they contain only strings, numbers, or tuples; if a tuple contains any mutable object either directly or indirectly, it cannot be used as a key. You can’t use lists as keys, since lists can be modified in place using index assignments, slice assignments, or methods like append() and extend().


In case you're wondering what the error message means, it's complaining because there's no built-in hash function for lists (by design), and dictionaries are implemented as hash tables.

How do I fix the error unhashable type: 'list' when trying to make a dictionary of lists?

list is an unhashable type, you need to convert it into tuple before using it as a key in dictionary:

>>> lst = [['25'], ['20','25','28'], ['27','32','37'], ['22']]
>>> print dict((tuple(l), len(l)) for l in lst)
{('20', '25', '28'): 3, ('22',): 1, ('25',): 1, ('27', '32', '37'): 3}

TypeError unhashable type 'list'

Your code, somewhere, is doing from re import search (or, shudders, from re import *) at global scope. You then fail to properly invoke search on self, so it finds the re module function and calls that instead. As a bonus error, you're not returning the values from your recursive calls, so any time you recurse you end up returning None. The fixes are pretty simple at least, just change:

search(nums, target)

to:

return self.search(nums, target)

invoking the correct search method on self, and returning what it returns. The fix must be made in both places you're making the recursive call.

json data throwing error as TypeError: unhashable type: 'list'

You can not insert an array to an object directly. You should use a key.

json_data = [
{
"guid": "DevPhase",
"attributeDefinitionId": "5578",
"values": [
{
"guid": "Active"
}
]
},

{
"guid": "Accessibility",
"attributeDefinitionId": "5590",
"values": [
{
"guid": "externalpublicnetwork"
}
]
},

{
"guid": "DevStrategy",
"attributeDefinitionId": "5583",
"values": [
{
"guid": "Internal"
}
]
}
]

Try with this.

How to fix 'TypeError: unhashable type: 'list' error?

You should call re.sub inside a list comprehension, not the other way around. Also, don't name a variable list since it would shadow the built-in function list. I've renamed it to lst in the following example:

updated_list = [re.sub('\d+', str(i), s, 1) for i in lst]


Related Topics



Leave a reply



Submit