简体   繁体   中英

Fast way to create a nested dictionary from a list of tuples without a for loop

I know similar questions have been asked before but I cannot find an adequate answer to my situation.

Let's say I have the following list of tuples:

d = [('first', 1), ('second', 2), ('third', 3)]

I can convert this very easily into a dictionary:

dict(d)
# {'first': 1, 'second': 2, 'third': 3}

Now, if I instead have the following list of tuples:

d = [('a', 'first', 1), ('a', 'second', 2), ('b', 'third', 3)]

How can I, most efficiently, get the following nested dictionary:

{'a': {'first': 1, 'second': 2}, 'b': {'third': 3}}

Here's the solution I have now:

from collections import defaultdict
dd = defaultdict(dict)
for a, b, c in d:
    dd[a][b] = c
# defaultdict(dict, {'a': {'first': 1, 'second': 2}, 'b': {'third': 3}})

Is this the most performant way of doing this? Is it possible to avoid the for loop? It's likely that I have to deal with cases where d is very large, and this method may not scale very well. This part is critical to a web application I am building, and that's why performance is very important.

Input/feedback/help is appreciated!

You can do with groupby

from itertools import groupby
result = {k:dict([(i[1],i[2]) for i in l]) for k, l in groupby(d, key=lambda x: x[0])}

# Result
{'a': {'first': 1, 'second': 2}, 'b': {'third': 3}}

When you use groupby the values are expected to sort with key.

Edit:

You can avoid the second looping by using map .

{k:dict(map(lambda x: (x[1],x[2]), l)) for k, l in groupby(d, key=lambda x: x[0])}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM