Dict comprehensions - improvement to docs?

Frank Millman frank at chagford.com
Mon Mar 16 02:45:46 EDT 2015


"Ian Kelly" <ian.g.kelly at gmail.com> wrote in message 
news:CALwzidnTUifj_L=DSH_8s+z0L44pxVvdpG1+pfz1Tzm=ECTLsw at mail.gmail.com...
> 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.
>
[...]
>
> 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
>

This is what the library reference says about lists -

"""
Lists may be constructed in several ways:

Using a pair of square brackets to denote the empty list: []
Using square brackets, separating items with commas: [a], [a, b, c]
Using a list comprehension: [x for x in iterable]
Using the type constructor: list() or list(iterable)
"""

This is what it says about dictionaries -

"""
Dictionaries can be created by placing a comma-separated list of key: value 
pairs within braces, for example: {'jack': 4098, 'sjoerd': 4127} or {4098: 
'jack', 4127: 'sjoerd'}, or by the dict constructor.

class dict(**kwarg)
class dict(mapping, **kwarg)
class dict(iterable, **kwarg)

Return a new dictionary initialized from an optional positional argument and 
a possibly empty set of keyword arguments.
"""

If you did not know about dict comprehensions, there is nothing to tell you 
that they even exist.

I feel that it should be mentioned.

Frank






More information about the Python-list mailing list