2

how can I convert a list of dict to a list of tuple? Input:

[{'x': 0.4711900100474648, 'y': 0.6294374442355883}, {'x': 0.4732473419066774, 'y': 0.629306809190704}, {'x': 0.47373722332499346, 'y': 0.6274779185623242}, {'x': 0.47363924704133026, 'y': 0.6273908285324014}, {'x': 0.4731493656230142, 'y': 0.6261715681134813}, {'x': 0.4722349203088243, 'y': 0.6252571227992915}, {'x': 0.47210428526394, 'y': 0.62521357778433}, {'x': 0.4709285698599815, 'y': 0.6253442128292143}, {'x': 0.47024273587433907, 'y': 0.62612802309852}, {'x': 0.4706019822477708, 'y': 0.6283052738465912}]

I want this:

[(0,47..., 0.62...),(...,...)]

I tried this:

tupleList = [tuple(val['x'], val['y']) for dic in listOfDict for key,val in dic.items()]

I get error TypeError: 'float' object is not subscriptable

otto
  • 1,815
  • 7
  • 37
  • 63
  • You can cut the problem to smaller pices. E.g. try `for dic in listOfDict:` `for key,val in dic.items():` `print(val)` to see that `val` is a single number and you cannot do `val["x"]` – Jan Stránský Sep 23 '20 at 09:14

2 Answers2

5
list_of_points = [{'x': 0.4711900100474648, 'y': 0.6294374442355883}, {'x': 0.4732473419066774, 'y': 0.629306809190704}, {'x': 0.47373722332499346, 'y': 0.6274779185623242}, {'x': 0.47363924704133026, 'y': 0.6273908285324014}, {'x': 0.4731493656230142, 'y': 0.6261715681134813}, {'x': 0.4722349203088243, 'y': 0.6252571227992915}, {'x': 0.47210428526394, 'y': 0.62521357778433}, {'x': 0.4709285698599815, 'y': 0.6253442128292143}, {'x': 0.47024273587433907, 'y': 0.62612802309852}, {'x': 0.4706019822477708, 'y': 0.6283052738465912}]

points_tuples = [(p['x'], p['y']) for p in list_of_points] 
Or Y
  • 2,088
  • 3
  • 16
5

If you are using python 3.7+ and inserted the keys in x,y order (like done here) you could simply use the dict.values() of each inner dict. The values() will be in key (==input) order as well:

data = [{'x': 0.4711900100474648, 'y': 0.6294374442355883}, 
        {'x': 0.4732473419066774, 'y': 0.629306809190704}, 
        {'x': 0.47373722332499346, 'y': 0.6274779185623242}, 
        {'x': 0.47363924704133026, 'y': 0.6273908285324014}, 
        {'x': 0.4731493656230142, 'y': 0.6261715681134813}, 
        {'x': 0.4722349203088243, 'y': 0.6252571227992915}, 
        {'x': 0.47210428526394, 'y': 0.62521357778433}, 
        {'x': 0.4709285698599815, 'y': 0.6253442128292143}, 
        {'x': 0.47024273587433907, 'y': 0.62612802309852}, 
        {'x': 0.4706019822477708, 'y': 0.6283052738465912}]

tup = [tuple(d.values()) for d in data]

Output:

[(0.4711900100474648, 0.6294374442355883), (0.4732473419066774, 0.629306809190704), 
 (0.47373722332499346, 0.6274779185623242), (0.47363924704133026, 0.6273908285324014), 
 (0.4731493656230142, 0.6261715681134813), (0.4722349203088243, 0.6252571227992915), 
 (0.47210428526394, 0.62521357778433), (0.4709285698599815, 0.6253442128292143), 
 (0.47024273587433907, 0.62612802309852), (0.4706019822477708, 0.6283052738465912)]
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
  • is it guaranteed that `x` comes before `y`? – Jan Stránský Sep 23 '20 at 09:13
  • 2
    @Jan with pyhton 3.8 the order of keys in dictionaries is guaranteed to be insert order - so if you inputtted them as x then y they will stay that way if you use the values() for that dict – Patrick Artner Sep 23 '20 at 09:14
  • I tried this. The answer is no, the order is not the same order as the keys in the dictionary. – Tony Jul 22 '23 at 19:23
  • @Tony check which version of python you are using. Look at the links given in the question [are-dictionaries-ordered-in-python-3-6](https://stackoverflow.com/questions/39980323/are-dictionaries-ordered-in-python-3-6) - for your convenience: [1](https://peps.python.org/pep-0468/) and [2](https://mail.python.org/pipermail/python-dev/2017-December/151283.html) - after making sure you use cpython 3.6+ or any python 3.7+ and verifying your "the order is not the same" result - file a bug report. – Patrick Artner Jul 23 '23 at 08:21