By reading a YAML file, I’d like to get the following python dictionary:
mydict = {('x','x'): 4}
or:
mydict = {['x','x']: 4}
Basically, I want a tuple or list as a key in a dictionary. This is the YAML I’ve tried:
mydict:
!!python/tuple [e,e]: 4 # valid, but python-specific
!!python/list [b,b]: 4 # error: "found unhashable key"
[c,c]: 4 # error: "found unhashable key"
!!seq [d,d]: 4 # error: "found unhashable key"
I’m using PyYAML, so either the YAML code has to be YAML1.1 syntax, or you could recommend me a better way to store this kind of data in a text file. I’ve thought of json.load, but JSON does not accept arrays as mapping keys.
Thanks =)
No way to use a list as a hash key in Python. It’s mutable. You have to use a tuple.