[Python-ideas] Dict(x, y) -> Dict(zip(x, y))

Bruce Leban bruce at leban.us
Sun Feb 14 21:43:29 EST 2016


this would have:

>>> dict(((1,2),(3,4)))
{1: 2, 3: 4}

>>> dict(((1,2),(3,4)), ((5,6)))
{(1, 2): 5, (3, 4): 6}

in other words, the interpretation of the first argument to dict is
entirely different if there is a second argument than if there isn't one.
It's quite reasonable to expect the reverse, namely that the result would
be:

>>> dict(((1,2),(3,4)), ((5,6)))
{1: 2, 3: 4, 5: 6}

So adding this saves 5 characters and makes it easier to confuse readers of
the code. Doesn't seem like a good idea to me.


--- Bruce
Check out my puzzle book and get it free here:
http://J.mp/ingToConclusionsFree (available on iOS)



On Sun, Feb 14, 2016 at 4:30 PM, Oscar Benjamin <oscar.j.benjamin at gmail.com>
wrote:

> I often find myself writing dict(zip(x, y)). Maybe that's just me or
> maybe not but I would like it if it were possible to spell that simply
> as dict(x, y).
>
> Currently dict() with two arguments is an error:
>
> $ python3
> Python 3.4.3 (default, Mar 26 2015, 22:03:40)
> [GCC 4.9.2] on linux
> Type "help", "copyright", "credits" or "license" for more information.
> >>> dict(['foo', 'bar'], [1, 2])
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> TypeError: dict expected at most 1 arguments, got 2
>
> I would prefer it if dict(x, y) would treat x as an iterable of keys
> and y as an iterable of corresponding values. This would be
> semantically equivalent to dict(zip(x, y)) except that it would raise
> an error if x and y don't yield the same number of items (zip
> truncates the longer argument).
>
> Although mostly equivalent the new construction might be more
> efficient, would reduce the visual noise of the redundant call to zip,
> and would be slightly more robust to errors.
>
> --
> Oscar
> _______________________________________________
> 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/20160214/db579cf4/attachment.html>


More information about the Python-ideas mailing list