I'm looking for an elegant way to encode values of dictionary objects to the string type. Like the following:
{"a":"b","c":1,"e":None,"g":True}
to
{"a":"b","c":"1","e":"None","g":"True"}
Thank you a lot!
simple with another dict comprehension:
a = {"a":"b","c":1,"e":None,"g":True}
a = {k:str(v) for k,v in a.items()}
or if you don't want to rebuild the dictionary, do it the old way:
for k in a.keys():
a[k] = str(a[k])
(BTW no fear of overhead of converting already existing str
to str
: Should I avoid converting to a string if a value is already a string?)