Dict comprehensions - improvement to docs?

Ian Kelly ian.g.kelly at gmail.com
Tue Mar 17 11:48:14 EDT 2015


On Tue, Mar 17, 2015 at 12:44 AM, Frank Millman <frank at chagford.com> wrote:
> Thanks for the explanation. I'll try not to make that mistake again.
>
> However, to go back to the original example, we want to compare a dict
> comprehension with a dict() constructor using a generator expression.
>
> Let's see if I have got this one right -
>
> C:\>python -m timeit -s "x=range(65, 91); y=[chr(z) for z in x]" "dict((a,
> b) for a, b in zip(x, y))"
> 10000 loops, best of 3: 49.6 usec per loop
>
> C:\>python -m timeit -s "x=range(65, 91); y=[chr(z) for z in x]" "{a: b for
> a, b in zip(x, y)}"
> 10000 loops, best of 3: 25.8 usec per loop

Why did you revert back to the no-op generator expression in the first
case instead of the more efficient dict(zip(x, y))?



More information about the Python-list mailing list