[issue23677] Mention dict and set comps in library reference
New submission from Frank Millman: This is from the documentation at Section 4.6.4. 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) """ Comprehensions are mentioned as a constructor. This is from the documentation at Section 4.10. Mapping Types """ 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. """ There is no mention of dictionary comprehensions. For consistency, I believe that the documentation for Dicts and Sets should mention comprehensions. ---------- assignee: docs@python components: Documentation messages: 238186 nosy: FrankMillman, docs@python priority: normal severity: normal status: open title: Mention dict and set comps in library reference versions: Python 3.4 _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue23677> _______________________________________
Changes by Mark Lawrence <breamoreboy@yahoo.co.uk>: ---------- keywords: +patch versions: +Python 3.5 Added file: http://bugs.python.org/file38507/issue23677.diff _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue23677> _______________________________________
R. David Murray added the comment: Sounds reasonable. Dict and set comprehensions were added later than list comprehensions, and we probably just didn't notice this needed updating. Mark's patch, however, is incorrect. Mark: the dict/set literal notation is a different thing from a comprehension. ---------- nosy: +r.david.murray _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue23677> _______________________________________
Mark Lawrence added the comment: That was embarrassing, hopefully this is rather better. ---------- nosy: +BreamoreBoy Added file: http://bugs.python.org/file38534/issue23677_v2.diff _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue23677> _______________________________________
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@bugs.python.org> <http://bugs.python.org/issue23677> _______________________________________
Changes by Ezio Melotti <ezio.melotti@gmail.com>: ---------- nosy: +ezio.melotti stage: -> patch review type: -> enhancement versions: +Python 3.6 -Python 3.4 _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue23677> _______________________________________
Martin Panter added the comment: The distiction about non-empty sets is a bit misleading. You can create an empty set via comprehension:
{x for x in ()} set()
---------- nosy: +martin.panter _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue23677> _______________________________________
Josh Rosenberg added the comment: Heck, with the addition of additional unpacking generalizations in 3.5, you can make an empty set even without a comprehension: {*()} Not really recommending the "one-eyed monkey operator", but the addition of unpacking generalizations undoes several of the limits on literals that previously existed. ---------- nosy: +josh.r _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue23677> _______________________________________
Change by Mark Lawrence <breamoreboy@gmail.com>: ---------- nosy: -BreamoreBoy _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue23677> _______________________________________
Cheryl Sabella <cheryl.sabella@gmail.com> added the comment: Assigning to @Mariatta for the CPython mentored sprint. ---------- assignee: docs@python -> Mariatta nosy: +Mariatta, cheryl.sabella stage: patch review -> needs patch versions: +Python 3.7, Python 3.8 -Python 3.5, Python 3.6 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue23677> _______________________________________
Change by Furkan Onder <furkanonder@protonmail.com>: ---------- nosy: +furkanonder nosy_count: 8.0 -> 9.0 pull_requests: +19337 stage: needs patch -> patch review pull_request: https://github.com/python/cpython/pull/20027 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue23677> _______________________________________
Furkan Onder <furkanonder@protonmail.com> added the comment: PR has been sent. ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue23677> _______________________________________
participants (8)
-
Cheryl Sabella
-
Ezio Melotti
-
Frank Millman
-
Furkan Onder
-
Josh Rosenberg
-
Mark Lawrence
-
Martin Panter
-
R. David Murray