Join us

YAML: while constructing a mapping found unhashable key

The error message "found unhashable key" occurs when trying to convert a mapping from YAML to a Python dict and the key is not hashable, which means it can't be used as a dictionary key.

This happens because mutable containers like lists are not hashable while immutable containers like tuples are hashable. If you control the input and are okay with language-specific tags, you can use full_load(), which resolves all tags except those known to be unsafe, including all the Python-specific tags.

If you don't control the input, you can use a custom constructor to convert the keys to something hashable, such as converting list keys to tuples.

If you don't know the key types in advance, you can let the mapping devolve into a list of pairs.

However, this method doesn't work if you need to round-trip the data or emit this kind of YAML yourself.


Let's keep in touch!

Stay updated with my latest posts and news. I share insights, updates, and exclusive content.

Unsubscribe anytime. By subscribing, you share your email with @faun and accept our Terms & Privacy.

Give a Pawfive to this post!


Only registered users can post comments. Please, login or signup.

Start writing about what excites you in tech — connect with developers, grow your voice, and get rewarded.

Join other developers and claim your FAUN.dev() account now!

Avatar

The FAUN

FAUN.dev()

@faun
The FAUN watches over the forest of developers. It roams between Kubernetes clusters, code caves, AI trails, and cloud canopies, gathering the signals that matter and clearing out the noise.
Developer Influence
3k

Influence

302k

Total Hits

3711

Posts