[Python-ideas] factory for efficient creation of many dicts with the same keys

Guido van Rossum guido at python.org
Fri Sep 8 10:56:14 EDT 2017


I think you've got it backwards -- if you send the patch the idea *may* be
accepted. You ought to at least show us the docs for your proposed factory,
it's a little murky from your example.

On Fri, Sep 8, 2017 at 6:34 AM, Sergey Fedoseev <fedoseev.sergey at gmail.com>
wrote:

> Hi all,
>
> Sometimes you may need to create many dicts with the same keys, but
> different
> values. For example, if you want to return data from DB as dicts.
>
> I think that special type could be added to solve this task more
> effectively.
> I created proof of concept for this and here's benchmarks:
>
> # currently the fastest way to do it AFAIK
> $ ./python -m timeit -s "nkeys = 5; nrows = 1000; rows = [(i,)*nkeys
> for i in range(nrows)]; enumerated = list(enumerate(range(nkeys)))"
> "for row in rows: {key: row[i] for i, key in enumerated}"
> 500 loops, best of 5: 645 usec per loop
>
> $ ./python -m timeit -s "nkeys = 5; nrows = 1000; rows = [(i,)*nkeys
> for i in range(nrows)]; factory = dict.factory(*range(nkeys)); from
> itertools import starmap" "for d in starmap(factory, rows): d"
> 5000 loops, best of 5: 81.1 usec per loop
>
> I'd like to write a patch if this idea will be accepted.
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>



-- 
--Guido van Rossum (python.org/~guido)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20170908/2798f344/attachment-0001.html>


More information about the Python-ideas mailing list