Home > Software engineering >  Converting list of strings into list of x,y tuples
Converting list of strings into list of x,y tuples

Time:10-23

I'm trying to convert ['25', '25', '10', '1', '50', '25', '140', '30'] into [(25,25),(10,1),(50,25),(140,30)] but not sure how. I tried something like a = [tuple(val) for val in w] but wasn't sure where to go from there.

Thanks

CodePudding user response:

Iterate through your list 2 items at a time:

data = ['25', '25', '10', '1', '50', '25', '140', '30']

l = []
for i in range(0, len(data), 2):
    l.append((data[i], data[i 1]))

print(l)

CodePudding user response:

To yield two items at the same time, you could try this:

data = ['25', '25', '10', '1', '50', '25', '140', '30']
result = list(zip(map(eval, data[0::2]), map(eval, data[1::2])))
  • Related