[Python-ideas] Dict literal use for custom dict classes
Jelte Fennema
me at jeltef.nl
Sun Dec 13 17:22:20 EST 2015
This is indeed another option, but it decouples the keys from the values.
Which makes it harder to see with a quick look what key will have what
value, you will have to count the keys. This feels to me like a serious
drawback of this method since knowing what value a key will have is pretty
important when talking about initializing dictionaries.
On 13 December 2015 at 22:10, Cody Piersall <cody.piersall at gmail.com> wrote:
>
>
> 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
>
>
> _______________________________________________
> 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/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20151213/00a0cff0/attachment.html>
More information about the Python-ideas
mailing list