given a list of python strings, how can I automatically convert them to their correct type? Meaning, if I have:
[\"hello\", \"3\", \"3.64\", \"-1\"] <
[\"hello\", \"3\", \"3.64\", \"-1\"]
I accomplished the same using json.loads method
json.loads
def f(l): for i in l: try: yield json.loads(i) except: yield i
Test:
In [40]: l Out[40]: ['hello', '3', '3.64', '-1'] In [41]: list(f(l)) Out[41]: ['hello', 3, 3.64, -1]