[docs] [issue23677] Mention dict and set comps in library reference

Frank Millman report at bugs.python.org
Wed Mar 18 07:33:43 CET 2015

Frank Millman added the comment:

Lists and tuples are described like this -

class list([iterable]) 
Lists may be constructed in several ways:

class tuple([iterable]) 
Tuples may be constructed in a number of ways:

I think a similar approach to Dicts and Sets could make sense -

class dict([**kwarg])
Dicts may be constructed in a number of ways:

- Using a pair of braces to denote the empty dict: {}
- Placing a comma-separated list of key: value pairs within braces: {'jack': 4098, 'sjoerd': 4127} or {4098: 'jack', 4127: 'sjoerd'}
- Using a dict comprehension: {k: v for k, v in iterable}
- Using the dict() built-in: dict() or dict(**kwarg) or dict(mapping, **kwarg) or dict(iterable, **kwarg) 

Add a new example -

f = {k: v for k, v in [('one', 1), ('two', 2), ('three', 3)]}

class set([iterable])
class frozenset([iterable])
Sets may be constructed in a number of ways:

- Non-empty sets (not frozensets) can be created by placing a comma-separated list of elements within braces, for example: {'jack', 'sjoerd'}
- Non-empty sets (not frozensets) can be created by using a set comprehension: {x for x in iterable}
- Using the set() or frozenset() built-in

The 'bullet-point' construction is not really necessary for Sets, but it would make it consistent with the others.

A related point (I can raise a separate Issue if preferred) -

For me, the power of comprehensions lies in their 'filtering' ability. This is not mentioned in any of the above examples, so newcomers may wonder why they should use them.

We don't want to make the examples too complicated. Maybe just add 'if ...' to the example, and provide a cross-reference to Section 6.2.4 in the Language Reference (Displays for lists, sets and dictionaries).


Python tracker <report at bugs.python.org>

More information about the docs mailing list