0

I have tried to find an answer to this in vain, so here goes:

The goal is to have a dictionary that has a few lists as values, and then have a function that (depending on user input) will take one of those lists and combine it with other lists, and finally I should get the final list printed.

Seems simple enough but what I get is a type error (lists being unhashable). The combine2 function seems to be working perfectly fine with any other two lists I try to feed it, except for when it tries to get a list that is a dictionary value (??). Does anybody know what I'm doing wrong?

dic = {
        'reptiles': ['lizzard', 'crocodile', 'T-Rex'],
        'birds': ['canary', 'parrot', 'seagul'],
        'mammals': ['monkey', 'cat', 'dog'],
        'insects': ['ant', 'bee', 'wasp']
        }

FishList = ['goldfish', 'shark', 'trout']


def combine2 (a, b):    # returns the combinations of 2 lists' items
    tmp = []
    n = 0
    while n < len(a):
        for i in b:
            if 8 <= len(str(a[n])+str(i)) and 16 >= len(str(a[n])+str(i)):
                tmp.append(str(a[n]) + str(i))
        n += 1
    return tmp



def animals_mix(k, l):      # just some arbitrary combinations of lists
    list1 = combine2(FishList, dic[k])
    list2 = combine2(list1, dic[k])
    list3 = combine2(dic[k], FishList)
    l = dic[k] + list1 + list2 + list3


def animals():
    print '''\n\nwhat's your favourite animal group?\n
    1) reptiles
    2) birds
    3) mammals
    4) insects
    '''

    while True:
        x = raw_input("[+] pick a number >  ")
        tmp = []
        if x == '1':
            animals_mix(dic['reptiles'], tmp)
            break
        elif x == '2':
            animals_mix(dic['birds'], tmp)
            break
        elif x == '3':
            animals_mix(dic['mammals'], tmp)
            break
        elif x == '4':
            animals_mix(dic['insects'], tmp)
            break
        elif x == '':
            break
        else:
            print "\nError: That wasn't in the list of options\nType one of the numbers or press ENTER to move on\n"
    return tmp


print animals()

1 Answer 1

1

For "TypeError: unhashable type: 'list'", it is because you are actually passing the list in your dict when you seemingly intend to pass the key then access that list:

animals_mix(dic['reptiles'], tmp)
...
def animals_mix(k, l):
    list1 = combine2(FishList, dic[k])

in the first line of animals_mix() you are actually trying to do dic[dic['reptiles']] and dicts can not be keyed by un-hashable types, hence the error.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.