Count - my function throws a "unhashable type: 'list'" error


Your function crashed on ([7.8, [], 7, 'buggy'], 7) as input because your function throws a "unhashable type: 'list'" error.

My solution is to using a dictionary to store the object in list as key and its count as value, but at last it can't handle the object '[]' as the key of the dictionary. Any ideas?

def count(sequence, item):
    t ={}
    for s in sequence:
        if s in t:
            t[s] = t[s]+1
            t[s] =1
    if item in t:
        return t[item]
        return 0


"unhashable type: 'list'"

You can't use a list as key of a dictionary.
like this..

d ={}
d[[1,2,3,4]] = "Ryan" # will throw an error

when It checks the code at input where a list contains another list,Then it throws that error.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.