简体   繁体   中英

Most efficient way to create a list of dictionaries in Python

Suppose I have a list of objects and for each of these objects I want to create a dictionary and put all the dictionaries I generated in a list.

What I am doing is the following:

def f(x):
    some function f that returns a dictionary given x

list_of_dict = []

xlist = [x1, x2, ..., xN]

for x in xlist:
    list_of_dict.append(f(x))

I am wondering whether there is a more efficient (faster) way to create a list of dictionaries than the one I am proposing.

Thank you.

Since you are dealing with HTTP requests (which is not obvious from your question), the rest of the answer is irrelevant: communications will dominate the computations by a sheer margin. I'll leave the answer here, anyway.


The original approach seems to be the slowest:

In [20]: %%timeit 
    ...: list_of_dict = [] 
    ...: for x in xlist: 
    ...:   list_of_dict.append(f(x)) 
    ...:                                                                        
13.5 µs ± 39.3 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

Mapping is the best way to go:

In [21]: %timeit list(map(f,xlist))                                             
8.45 µs ± 17 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

List comprehension is somewhere in the middle:

In [22]: %timeit [f(x) for x in xlist]                                          
10.2 µs ± 22.8 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

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