-1

So I have a list like so:

data= ['[a, b], [h,g]', '[j,f], [k,g]', '[g,h], [p,e]', '[u,f], [i,t]', '[o,g], [u,d]', '[g,f], [j,f]']

How can I wrap the 2 items in the list between '' with [] like so:

data = [['[a, b], [h,g]'], ['[j,f], [k,g]'], ['[g,h], [p,e]'], ['[u,f], [i,t]'], ['[o,g], [u,d]'], ['[g,f], [j,f]']]

I've tried:

coords = [[x] for x in data[::2]]

but this just wraps the whole list in a [] and not the 2 items :/

Real data:

real data

data = ['[53.50119612705815, -1.1270833894501477], [53.68387, -0.35847]', '[53.50119612705815, -1.1270833894501477], [53.68387, -0.35847]', '[54.89272380289834, -2.8951219798364622], [54.94804, -1.90475]', '[53.50119612705815, -1.1270833894501477], [53.38299, -3.05853]', '[53.50119612705815, -1.1270833894501477], [53.38299, -3.05853]', '[53.50119612705815, 
-1.1270833894501477], [53.38299, -3.05853]', '[54.89272380289834, -2.8951219798364622], [55.84457, -4.41674]', '[54.89272380289834, -2.8951219798364622], [55.84457, -4.41674]', '[54.89272380289834, -2.8951219798364622], [55.84457, -4.41674]', '[54.89272380289834, -2.8951219798364622], [55.06442, -3.65683]', '[54.89272380289834, -2.8951219798364622], [55.06442, -3.65683]', '[54.89272380289834, -2.8951219798364622], [55.06442, -3.65683]', '[54.89272380289834, -2.8951219798364622], [55.06442, -3.65683]']
RedRum
  • 902
  • 1
  • 13
  • 26
  • 2
    `[[i] for i in data]` will do…?! `'a, b'` aren't two items, it's one item. – deceze May 12 '22 at 12:52
  • Does this answer your question? [Pairs from single list](https://stackoverflow.com/questions/4628290/pairs-from-single-list) – Altareos May 12 '22 at 12:52
  • I think I may have put the question wrong, I have redone the question – RedRum May 12 '22 at 12:54
  • 1
    The first comment (and the only answer) is still a valid answer because each element is still a string and ends up as a list containing that string. – quamrana May 12 '22 at 12:58
  • 1
    How did you end up with this value? The problem you're trying to solve is probably a bit earlier in the code; post the code that's setting or reading in the `data` variable? – Jiří Baum May 12 '22 at 13:11

1 Answers1

1

Just doing this:

coords = [[x] for x in data]
Palinuro
  • 184
  • 1
  • 1
  • 15
  • this just wraps the whole list in ```[]``` The real data is : – RedRum May 12 '22 at 12:58
  • This answer is still a correct answer to the updated question. – quamrana May 12 '22 at 12:59
  • I think something funny is happening with my data. so your answer probs works, thanks! – RedRum May 12 '22 at 12:59
  • It is just wrapping the whole list in another ```[]``` .. I've added the real data to the original question – RedRum May 12 '22 at 13:03
  • Hi again, i'm soo sorry, I've tried your code in a seperate .py file with a small portion of my data and it works! so there is something wrong with my data format... many thanks! =D – RedRum May 12 '22 at 13:06