Convert a 3-item list to a dictionary

If I have two lists of tuples

tuple2list=[(4, 21), (5, 10), (3, 8), (6, 7)] tuple3list=[(4, 180, 21), (5, 90, 10), (3, 270, 8), (6, 0, 7)] 

How to convert it to a dictionary as shown below

 tuple2list2dict={4:21, 5:10, 3:8, 6:7} tuple3list2dict={4: {180:21}, 5:{90:10}, 3:{270:8}, 6:{0:7}} 

I know how to do this for 2 elements in tuples using

 tuple2list2dict=dict((x[0], index) for index,x in enumerate(tuple2list)) 

But for 3 elements I have a problem, I have an error when trying below,

 tuple3list2dict=dict((x[0], dict(x[1], index)) for index,x in enumerate(tuple3list)) 

How to reuse the above code for a 3-element tuple to create a dictionary?

Any pointer rated or pointed me to where I could find out more about this. Cannot find it on the Internet.

+4
source share
3 answers

In Python2.7 or later, you can use dict comprehension:

 In [100]: tuplelist = [(4, 180, 21), (5, 90, 10), (3, 270, 8), (4, 0, 7)] In [101]: tuplelist2dict = {a:{b:c} for a,b,c in tuplelist} In [102]: tuplelist2dict Out[102]: {3: {270: 8}, 4: {0: 7}, 5: {90: 10}} 

In Python2.6 or later, the equivalent will be

 In [26]: tuplelist2dict = dict((a,{b:c}) for a,b,c in tuplelist) 

Note that if the first value in tuples occurs more than once (as in the example above), the resulting tuplelist2dict contains only one key-value pair corresponding to the last tuple with a shared key.

+6
source

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 ]) # hello car, hi cdr {3: (270, 8), 4: (0, 7), 5: (90, 10)} 

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]} 
+2
source

If you need a nested dictionary without overriding the dictionary, you can use defaultdict from the collections library.

 >>> from collections import defaultdict >>> # Edited the list a bit to show when overrides >>> tuple3list=[(4, 180, 21), (4, 90, 10), (3, 270, 8), (6, 0, 7)] >>> tuple3dict = defaultdict(dict) >>> for x, y, z in tuple3list: ... tuple3dict[x][y] = z ... >>> print(tuple3dict) defaultdict(<class 'dict'>, {4: {180: 21, 90: 10}, 3: {270: 8}, 6: {0: 7}}) >>> tuple3dict[4][90] 10 

Unfortunately, one line assignment is difficult or impossible, so I assume that this is the only correct solution.

0
source

All Articles