export – Exporting Associations (containing lists and strings as keys and values) to python dictionaries

Since Mathematica Associations are similar objects to python dictionaries I think there should be an easy way to export a Mathematica Associations such that it can easily be loaded into python as a dictionary.

How do I do this?

I generally see two methods. 1.) Export and import as JSON. 2.) Export as PythonExpression. With both I ran into issues. The issue I ran into with the second method is that the PythonExpression actually seems to contain an illegal python expression since it uses a list as a key where it should be using a tuple. I guess that the method of conversion depends a bit on the kind of structures that you want to export. In my case the Associations contain lists of real numbers as keys and Real numbers or lists of Real numbers as values. Converting the Real numbers to floats is fine since I do not require more precision.

Example of attempt:

Export(file, <|{0.52, 1.4} -> 5.1423545`|>, "PythonExpression")

Creates a file with {(0.52, 1.4): 5.1423545}. Reading it in python with

with open(file,"r") as f:
    data = ast.literal_eval(f.read())

Gives the error:

TypeError: unhashable type: 'list'