I’m new to python and experimenting a bit but having trouble making a list into a tuple to use as a dictionary key. Here’s an example, which should make it more clear:
dict_of_lists_values = {}
dict_of_lists_values[('dog', 'cat')] = 10
dict_of_lists_values[('dog1', 'cat1')] = 10
dict_of_lists_values[('dog1', 'cat2')] = 10
dict_of_lists_values
{('dog', 'cat'): 10, ('dog1', 'cat2'): 10, ('dog1', 'cat1'): 10}
This works perfectly, and allows me to have a two values I can use as keys in a dictionary. When I try to apply this to a list, I get an error: TypeError: unhashable type: 'list'
dict_of_lists_values = {}
a = [22, 39, 0]
b = [15, 38, 12]
dict[(a, b)] = 'please work'
Based on my previous experiment, I think if I convert the list into a string it would work but I want it as it as a list not a string.
Is this possible?
Call
tuple()on a list to create a tuple with the elements in the list.