Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting List of 3 Element Tuple to Dictionary

Tags:

python

If I have two List 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 do I convert it to a dictionary as 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 it for 2 elements in tuples, using,

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

But for 3 elements I have problem, have error trying the below,

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

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

Any pointer appreciated or point me where I could read more on this. Have trouble finding it in the internet.

like image 518
Saravanan K Avatar asked Jan 10 '13 17:01

Saravanan K


People also ask

How do you convert a tuple to a dictionary?

In Python, use the dict() function to convert a tuple to a dictionary. A dictionary object can be created with the dict() function. The dictionary is returned by the dict() method, which takes a tuple of tuples as an argument. A key-value pair is contained in each tuple.

Can you have 3 items in a dictionary Python?

You can add as many items as you like.

Can we convert list into tuple and tuple into list?

Using the tuple() built-in function An iterable can be passed as an input to the tuple () function, which will convert it to a tuple object. If you want to convert a Python list to a tuple, you can use the tuple() function to pass the full list as an argument, and it will return the tuple data type as an output.


3 Answers

In Python2.7 or newer, you could use a 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 older, the equivalent would be

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

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

like image 140
unutbu Avatar answered Sep 30 '22 23:09

unutbu


This pair-case is simple, since it aligns with dict construction:

... the positional argument must be an iterator object. Each item in the iterable must itself be an iterator with exactly two objects. The first object of each item becomes a key in the new dictionary, and the second object the corresponding value.

>>> t = [(4, 21), (5, 10), (3, 8), (4, 7)]
>>> dict(t)
{3: 8, 4: 7, 5: 10}

The triple case could 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 bit more general:

>>> dict([ (k[0], k[1:]) for k in t ]) # hello car, hi cdr
{3: (270, 8), 4: (0, 7), 5: (90, 10)}

Note that your code:

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

is really just a confusing 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 comprehension:

>>> 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]}
like image 30
miku Avatar answered Sep 30 '22 23:09

miku


If one wants a nested dictionary without overriding the dictionary, could use the 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 assignments are tricky or impossible, hence I assume the only valid solution would be this.

like image 44
Grzegorz Avatar answered Oct 01 '22 00:10

Grzegorz