Making a dict from two lists/tuples

Emile van Sebille emile at fenx.com
Thu May 24 09:08:08 EDT 2001


"Alex Martelli" <aleaxit at yahoo.com> wrote in message
news:9eip7i0s67 at enews1.newsguy.com...

<snip  --this-->
> for k, v in zip(keys, vals):
>     d1[k] = v

<snip --vs-->
> map(d1.setdefault, keys, vals)

> D:\Python21>python po.py
> Plain: 0.126390609312
> Fancy: 0.0406811366581
>
>
> The fancy/neat form seems to be better than
> three times faster on my box, so it may be
> worth using DESPITE its fanciness...:-).
>

I think you mean that it may be worth doing BECAUSE of the SPEED increase.
One common argument is that optimizing in advance of profiling and despite
clarity is simply wasting time.  In particular, this idiom depends on a side
effect of the function argument, and throws away the normal result of map.
That's hardly intuitive behavior, and reeks somewhat of bad oysters (;-)),
although it may be proscribed standard practice at some shops.  The
technique of iterating through the indexes (i.e. for i in range(len(keys)):
d1[keys[i]] = vals[i]) is clearer, and is still about 2 1/2 times faster
than using zip.


--

Emile van Sebille
emile at fenx.com

---------





More information about the Python-list mailing list