Back to questions
JuniorTheoryCommon
What does the program print?
import json
data = {"id": 1, "tags": ("a", "b")}
back = json.loads(json.dumps(data))
print(back["tags"], back == data)['a', 'b'] False
- Expecting
('a', 'b') True, as if JSON kept Python types: JSON has only arrays, sodumpswrites the tuple as one andloadsreads it back as a list. - Expecting
Truebecause the values look the same: a list never equals a tuple,['a', 'b'] == ('a', 'b')isFalse, so the two dicts differ.
- What happens to an
intkey such as{1: "x"}after the same round trip? - What does
json.dumps({"tags": {"a"}})do with a set?