I have the code
l = [('1',['3','1','2']),('2',['4','5','2'])]
How do I make it:
l = [(1,[3,1,2]),(2,[4,5,2])]
I have the code
l = [('1',['3','1','2']),('2',['4','5','2'])]
How do I make it:
l = [(1,[3,1,2]),(2,[4,5,2])]
You can use int
function while iterating using list comprehension
r = [tuple([int(i),[int(k) for k in j]]) for i,j in l]
This can be generalised using recursion:
def to_ints(item):
if isinstance(item, list):
return [to_ints(e) for e in item]
elif isinstance(item, tuple):
return tuple(to_ints(e) for e in item)
else:
return int(item)
A simple approach is to remove the quotes and parse it back to a list
l = eval(str(l).replace("'", ""))