I have a list which looks something like this:
[\'1\', \'2\', \'3.4\', \'5.6\', \'7.8\']
How do I change the first two to int
int
Use a helper function:
def int_or_float(s): try: return int(s) except ValueError: return float(s)
Then use a list comprehension to apply the function:
[int_or_float(el) for el in lst]