Dict comprehensions - improvement to docs?

Ian Kelly ian.g.kelly at gmail.com
Mon Mar 16 02:17:36 EDT 2015


On Sun, Mar 15, 2015 at 11:25 PM, Frank Millman <frank at chagford.com> wrote:
> Hi all
>
> I like dict comprehensions, but I don't use them very often, so when I do I
> need to look up the format.

This surprises me, because I find the syntax very intuitive, at least
if you regularly use other kinds of comprehensions. If you don't often
use comprehensions at all, then I suppose there could be a memory
curve.

[x] is a list. [x for x in y] is a list comprehension. {x:y} is a
dict. {x:y for x,y in z} is a dict comprehension.

> I always struggle to find the information in the Library Reference. The
> obvious location, Mapping Types, shows various constructors, but not the
> comprehension.
>
> https://docs.python.org/3/library/stdtypes.html#mapping-types-dict
>
> So I turn to Google. It shows a couple of StackOverflow questions, and then
> a link to the Data Structures section of the Tutorial, which explains it
> succinctly.
>
> https://docs.python.org/3/tutorial/datastructures.html#dictionaries
>
> I feel that the Library Reference should be updated to include dict
> comprehensions.

Dict comprehensions aren't covered in the library reference because
they're a language feature, not a part of the standard library. The
syntax is defined at:

https://docs.python.org/3/reference/expressions.html#dictionary-displays

And an example usage can be found in the tutorial at the link you posted above.



More information about the Python-list mailing list