Reputation: 1871
How to convert the tuple:
t=(('1','a'), ('1','A'), ('2','b'), ('2','B'), ('3','c'),('3', 'C'))
into a dictionary:
{'1':('a','A'),'2':('b','B'),'3':('c','C')}
I have tried in a console:
>>> d={}
>>> t[0]
(1, 'a')
>>> d[t[0][0]]=t[0][1]
>>> d
{1: 'a'}
>>> t[0][0] in d
True
>>> d[t[1][0]]=t[1][1]
>>> d
{1: 'A'}
>>> d[t[0][0]]=t[0][1]
>>> d[t[1][0]]=d[t[1][0]],t[1][1]
>>> d
{1: ('a', 'A')}
Now the following script fails doing the job:
t=(('1','a'), ('1','A'), ('2','b'), ('2','B'), ('3','c'),('3', 'C'))
print "{'1':('a','A'),'2':('b','B'),'3':('c','C')} wanted, not:",dict(t)
d={}
for c, ob in enumerate(t):
print c,ob[0], ob[1]
if ob[0] in d:
print 'test'
d[ob[0]]=d[ob[0]],ob[1]
print d
else:
print 'else', d, ob[0],ob[1]
d[ob[0]]=d[ob[1]] # Errror is here
print d
print d
I have the error:
Traceback (most recent call last):
File "/home/simon/ProjetPython/python general/tuple_vers_dic_pbkey.py", line 20, in <module>
d[ob[0]]=d[ob[1]]
KeyError: 'a'
It seems to be different from $>>> d[t[0][0]]=t[0][1]$ . Thanks for your help
JP
PS Is there a better way to do the convertion?
Upvotes: 5
Views: 6264
Reputation: 35522
To be complete, you can use a side effect of a list comprehension to do this in one line:
>>> tups=t=(('1','a'), ('1','A'), ('2','b'), ('2','B'), ('3','c'),('3', 'C'))
>>> d={}
>>> [d.setdefault(k,[]).append(v) for k,v in tups]
[None, None, None, None, None, None]
>>> d
{'1': ['a', 'A'], '3': ['c', 'C'], '2': ['b', 'B']}
Or side-effect of a set comprehension (Py 2.7+ or 3.1+):
>>> d={}
>>> {d.setdefault(k,[]).append(v) for k,v in tups}
set([None])
>>> d
{'1': ['a', 'A'], '3': ['c', 'C'], '2': ['b', 'B']}
This is more for interest -- not recommended syntax -- but interesting nonetheless.
Upvotes: 0
Reputation: 1961
A super clean and elegant option would be to do the following:
>>> d = {}
>>> for k,v in (('1','a'), ('1','A'), ('2','b'), ('2','B'), ('3','c'),('3', 'C')):
... d.setdefault(k, []).append(v)
...
>>> d
{'1': ['a', 'A'], '3': ['c', 'C'], '2': ['b', 'B']}
Upvotes: 1
Reputation: 213223
>>> t=(('1','a'), ('1','A'), ('2','b'), ('2','B'), ('3','c'),('3', 'C'))
>>> d1 = {}
>>> for each_tuple in t:
if each_tuple[0] in d1:
d1[each_tuple[0]] = d1[each_tuple[0]] + list(each_tuple[1])
else:
d1[each_tuple[0]] = list(each_tuple[1])
>>> d1
{'1': ['a', 'A'], '3': ['c', 'C'], '2': ['b', 'B']}
Upvotes: 0
Reputation: 214949
You can use defaultdict from the collections module (although it will work better for lists, not tuples):
t=(('1','a'), ('1','A'), ('2','b'), ('2','B'), ('3','c'),('3', 'C'))
from collections import defaultdict
d = defaultdict(list)
for k, v in t:
d[k].append(v)
d = {k:tuple(v) for k, v in d.items()}
print d
or simply add tuples together:
t = (('1','a'), ('1','A'), ('2','b'), ('2','B'), ('3','c'),('3', 'C'))
d = {}
for k, v in t:
d[k] = d.get(k, ()) + (v,)
print d
Upvotes: 6
Reputation: 11467
t = (('1','a'), ('1','A'), ('2','b'), ('2','B'), ('3','c'),('3', 'C'))
foo = {}
for i in t:
if i[0] not in foo:
foo[i[0]] = [i[1]]
else:
foo[i[0]].append(i[1])
foo # {'3': ['c', 'C'], '2': ['b', 'B'], '1': ['a', 'A']}
Upvotes: 1
Reputation: 212835
import itertools as it
t=(('1','a'), ('1','A'), ('2','b'), ('2','B'), ('3','c'),('3', 'C'))
{k:tuple(x[1] for x in v) for k,v in it.groupby(sorted(t), key=lambda x: x[0])}
returns
{'1': ('A', 'a'), '2': ('B', 'b'), '3': ('C', 'c')}
Upvotes: 3