Reputation: 17238
I have following list of items (key-value pairs):
items = [('A', 1), ('B', 1), ('B', 2), ('C', 3)]
What I want to get:
{
'A' : 1,
'B' : [1,2]
'C' : 3
}
My naive solution:
res = {}
for (k,v) in items:
if k in res:
res[k].append(v)
else:
res[k] = [v]
I'm looking for some optimised more pythonic solution, anyone?
Upvotes: 4
Views: 5952
Reputation: 46
To convert dictionary to list of items
dict_items = list(dict_1.items())
To convert the list of items back to the dictionary
dict2 = dict(dict_items)
Upvotes: 1
Reputation: 1416
Maybe it looks ugly, but it works.
In [1]: items = [('A', 1), ('B', 1), ('B', 2), ('C', 3)]
In [2]: d = {}
In [3]: map(lambda i: d.update({i[0]: i[1] if d.get(i[0], i[1]) == i[1] else [d[i[0]], i[1]]}), items)
Out[3]: [None, None, None, None]
In [4]: print d
{'A': 1, 'C': 3, 'B': [1, 2]}
In else
branch we can check if d[i[0]]
returns a list.
Upvotes: 0
Reputation: 44444
Use could use defaultdict here.
from collections import defaultdict
res = defaultdict(list)
for (k,v) in items:
res[k].append(v)
# Use as dict(res)
EDIT:
This is using groupby, but please note, the above is far cleaner and neater to the eyes:
>>> data = [('A', 1), ('B', 1), ('B', 2), ('C', 3)]
>>> dict([(key,list(v[1] for v in group)) for (key,group) in groupby(data, lambda x: x[0])])
{'A': [1], 'C': [3], 'B': [1, 2]}
Downside: Every element is a list. Change lists to generators as needed.
To convert all single item lists to individual items:
>>> res = # Array of tuples, not dict
>>> res = [(key,(value[0] if len(value) == 1 else value)) for key,value in res]
>>> res
[('A', 1), ('B', [1, 2]), ('C', 3)]
Upvotes: 6
Reputation: 82550
This is very easy to do with defaultdict
, which can be imported from collections
.
>>> from collections import defaultdict
>>> items = [('A', 1), ('B', 1), ('B', 2), ('C', 3)]
>>> d = defaultdict(list)
>>> for k, v in items:
d[k].append(v)
>>> d
defaultdict(<class 'list'>, {'A': [1], 'C': [3], 'B': [1, 2]})
You can also use a dictionary comprehension:
>>> d = {l: [var for key, var in items if key == l] for l in {v[0] for v in items}}
>>> d
{'A': [1], 'C': [3], 'B': [1, 2]}
Upvotes: 0
Reputation: 174662
If you don't want to use defaultdict
/groupby
, the following works:
d = {}
for k,v in items:
d.setdefault(k, []).append(v)
Upvotes: 1
Reputation: 2341
You can use the WebOb multidict implementation:
>>> from webob import multidict
>>> a = multidict.MultiDict(items)
>>> a.getall('B')
[1,2]
Upvotes: 0