qfd
qfd

Reputation: 788

create dictionary using nested for loops

I am looking to create a dictionary from nested loops, that iterate through dates and variables. I would like to create this as efficiently as possible given the length of my dates is ~ 400 and myvars ~ 50,000. many thanks.

fmap = dict.fromkeys(range(len(dates)*len(myvars)))
count = 0
for j in dates:
    for i in myvars:
        fmap[count] = partial(dosomething, i, j)
        count = count+1

Upvotes: 1

Views: 80

Answers (1)

dome
dome

Reputation: 841

You can do this, it should be quite efficient:

count = len(dates) * len(myvars)
fmap = dict(zip(range(count), [partial(dosomething, i, j) for j in dates for i in myvars]))

Upvotes: 1

Related Questions