Multiplying all the values in a dictionary

Felipe Almeida Lessa felipe.lessa at
Fri Mar 24 10:16:21 CET 2006

Em Qui, 2006-03-23 às 21:23 -0800, Adam DePrince escreveu:
> Wait!  It occured to me.  Why are we touching the key at all.  This is
> a dictionary with mutable values.

This idea occured to me but I always forget about the [:] trick, so I
didn't try it :).

> Now, we *know* that all of the values are lists of length 2, so why not
> say this instead:
>  python2.4 -mtimeit -s 'from numarray import array; d =
> {(100,500):[5,5], (100,501):[6,6],
> (100,502):[7,7]}; x = dict(d);j=[0,0]' 'for i in x.values():
>  j[0]*=1;j[1]*=1'
> 1000000 loops, best of 3: 1.7 usec per loop

Why do you import numarray? o.O

Nice approach, though.


More information about the Python-list mailing list