[Python-ideas] Dict literal use for custom dict classes

Cody Piersall cody.piersall at gmail.com
Sun Dec 13 16:10:37 EST 2015


On Sun, Dec 13, 2015 at 5:43 AM, Laura Creighton <lac at openend.se> wrote:
>
> I care about readability but I find:
>
> d = OrderedDict()
> for key, value in zip([1, 4, 2], ['a', int, (3, 3)]):
>     d[key] = value
>
> quite readable.
>
> Laura

You don't even need the loop, just the zip.

>>> from collections import OrderedDict
>>> OrderedDict(zip([5, 9, 3, 53, 2342, 'pizza'], 'abcdef'))
OrderedDict([(5, 'a'), (9, 'b'), (3, 'c'), (53, 'd'), (2342, 'e'),
('pizza', 'f')])

But it's probably more readable as
>>> keys = [5, 9, 3, 53, 2342, 'pizza']
>>> values = 'abcdef'
>>> OrderedDict(zip(keys, values))
OrderedDict([(5, 'a'), (9, 'b'), (3, 'c'), (53, 'd'), (2342, 'e'),
('pizza', 'f')])

Cody
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20151213/27fd5fa6/attachment.html>


More information about the Python-ideas mailing list