I have a python dictionary with keys being tuples (in fact, tuples of tuples of…
- User
I have a python dictionary with keys being tuples (in fact, tuples of tuples of 2d-coordinates), which I want to save, probably as a json file. But json file doesn't accept tuples as key. Two ways of deal with this problem are: (1) defining a function to convert a tuple to a string for saving, and another reverse one for loading, (2) saving as list of dictionaries, each being {"key": orig_tuple_key, "value": orig_value}. If my ultimate goal is to loop through the tuple keys in some other operations, which method has a 'better' time complexity? Or are there other alternatives to this saving and loading problem?
- Assistant
In JSON an “object key” has to be a string, so as soon as you decide to stay with JSON you must transform the tuple somehow.
The two variants you sketched both work, but neither of them is fundamentally faster in big-O terms: for n dictionary entries that contain on average L numbers inside the nested tuples they all touch every element once → Θ(n · L) for dump and the same again for load.
What does differ is the constant factor and the code readability / robustness.─────────────────────────────────
-
Turning the key into a string ───────────────────────────────── dump:
json_dict = {repr(k): v for k, v in orig.items()} json_str = json.dumps(json_dict)
load:
tmp = json.loads(json_str) restored = {ast.literal_eval(k): v for k, v in tmp.items()}• You pay for turning every number into characters twice
(“(" , "1" , "," , "2" , …).
• You also needast.literal_eval(or similar) which is noticeably slower than a meretuple(list_)cast because it has to run a miniature Python parser.
• Potential collision: two different tuple objects that happen to have the samerepr/strwould overwrite each other.──────────────────────────────────────────── 2) Storing a list of {"key": …, "value": …} ──────────────────────────────────────────── Here the key is stored as data instead of as an object field name, so you can keep it numeric (turn the tuples into lists – JSON has no tuple literal):
dump_blob = [ {"key": [list(p) for p in key_tuple], "value": value} for key_tuple, value in orig.items() ] json_str = json.dumps(dump_blob)load:
tmp = json.loads(json_str) restored = {tuple(map(tuple, entry["key"])): entry["value"] for entry in tmp}• Still Θ(n · L), but the constants are better than variant 1: no parsing of Python literals, no extra parentheses/commas/whitespace in the file, and
tuple(list_)is cheap.
• File is usually smaller and faster to read/write.
• Requires one more pass if you want a dict again (forloop above).───────────────────────────────── 3) A different serializer altogether ───────────────────────────────── If you do not need the file to be human-readable JSON, pick something that already understands Python data structures:
• pickle / dill
with open("data.pkl", "wb") as f:
pickle.dump(orig, f)with open("data.pkl", "rb") as f: restored = pickle.load(f)– Handles tuple keys natively, no transformation, still Θ(n · L) but with the smallest constant factor.
– File is Python-specific and must not be loaded from untrusted sources.• msgpack, cbor2, bson, … – same idea but cross-language binary formats.
• If the data is large or you need random access: SQLite, HDF5, Parquet…
───────────────────────────────── So, which should you pick? ───────────────────────────────── • If you truly need JSON (hand-edited config, exchange with JS, etc.) → go with variant 2 (list of records). It is simpler, safer, and faster than turning tuples into strings.
• If the file is only ever consumed by Python code → use pickle or msgpack and be done. They keep the exact data structure and are the quickest in practice.
Either way, the asymptotic time complexity is the same; the decision is about constant factors, file size, readability and security.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.