python: unhashable type error
counter[row[11]]+=1
You don't show what data
is, but apparently when you loop through its rows, row[11]
is turning out to be a list
. Lists are mutable objects which means they cannot be used as dictionary keys. Trying to use row[11]
as a key causes the defaultdict
to complain that it is a mutable, i.e. unhashable, object.
The easiest fix is to change row[11]
from a list
to a tuple
. Either by doing
counter[tuple(row[11])] += 1
or by fixing it in the caller before data
is passed to medications_minimum3
. A tuple simply an immutable list, so it behaves exactly like a list does except you cannot change it once it is created.
As Jim Garrison said in the comment, no obvious reason why you'd make a one-element list out of drug.upper()
(which implies drug is a string).
But that's not your error, as your function medications_minimum3()
doesn't even use the second argument (something you should fix).
TypeError: unhashable type: 'list'
usually means that you are trying to use a list as a hash argument (like for accessing a dictionary). I'd look for the error in counter[row[11]]+=1
-- are you sure that row[11]
is of the right type? Sounds to me it might be a list.
I don't think converting to a tuple is the right answer. You need go and look at where you are calling the function and make sure that c
is a list of list of strings, or whatever you designed this function to work with
For example you might get this error if you passed [c]
to the function instead of c