Making a dict from two lists/tuples

gzeljko gzeljko at sezampro.yu
Thu May 24 09:37:54 EDT 2001


From: Alex Martelli <aleaxit at yahoo.com>
> 
> As I suggested in a followup, but just to complete and clarify:
>     dict = {}
>     map(dict.setdefault, keys, values)
> seems a reasonable way to do it.
> 
> I will confess that I, personally, would tend to use the alternative,
> "less neat" (but more likely to be understandable by a reader who
> still hasn't had his or her morning coffee...)
>     dict = {}
>     for k, v in zip(keys, values):
>         dict[k] = v
> 
> However...:
> 
> import time
> 
> keys = range(10000)
> vals = range(10000)
> 
> start = time.clock()
> d1 = {}
> for k, v in zip(keys, vals):
>     d1[k] = v
> stend = time.clock()
> 
> print "Plain:", stend-start
> 
> start = time.clock()
> d1 = {}
> map(d1.setdefault, keys, vals)
> stend = time.clock()
> 
> print "Fancy:", stend-start
> 
> 
> D:\Python21>python po.py
> Plain: 0.12956698978
> Fancy: 0.0406777842777
> 
> 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...:-).
> 

Until xzip.

BTW, what's wrong with x<name> ?
Seems it's abondoned by iterrator group.

gzeljko

> 
> Alex
> 
> 
> 
> -- 
> http://mail.python.org/mailman/listinfo/python-list
> 






More information about the Python-list mailing list