This pairing case is simple, as it aligns with the dict construct :
... the positional argument must be an iterator object. Each element in iterable must itself be an iterator with exactly two objects . The first object of each element is the key in the new dictionary, and the second object is the corresponding value.
>>> t = [(4, 21), (5, 10), (3, 8), (4, 7)] >>> dict(t) {3: 8, 4: 7, 5: 10}
The triple case can be solved in this way:
>>> t = [(4, 180, 21), (5, 90, 10), (3, 270, 8), (4, 0, 7)] >>> dict([ (k, [v, w]) for k, v, w in t ]) {3: [270, 8], 4: [0, 7], 5: [90, 10]}
Or a little more general:
>>> dict([ (k[0], k[1:]) for k in t ])
Please note that your code is:
_3_tuplelist_to_dict = {4: {180:21}, 5:{90:10}, 3:{270:8}, 4:{0:7}}
really just a confused representation of this:
{3: {270: 8}, 4: {0: 7}, 5: {90: 10}}
Try:
>>> {4: {180:21}, 5:{90:10}, 3:{270:8}, 4:{0:7}} == \ {3: {270: 8}, 4: {0: 7}, 5: {90: 10}} True
With Python 3, you can use a dict understanding:
>>> t = [(4, 180, 21), (5, 90, 10), (3, 270, 8), (4, 0, 7)] >>> {key: values for key, *values in t} {3: [270, 8], 4: [0, 7], 5: [90, 10]}