You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Mention dict and set comps in library reference #67865
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee='https://github.com/Mariatta'closed_at=Nonecreated_at=<Date2015-03-16.08:50:55.042>labels= ['3.8', 'type-feature', '3.7', 'docs']
title='Mention dict and set comps in library reference'updated_at=<Date2020-05-10.18:23:31.452>user='https://bugs.python.org/FrankMillman'
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.
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).
The distiction about non-empty sets is a bit misleading. You can create an empty set via comprehension:
>>> {x for x in ()}
set()
MojoVampire commented on Jan 5, 2016
MojoVampiremannequin
Mannequin
More actions
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.
Activity
frankmillman commentedon Mar 16, 2015
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.
There is no mention of dictionary comprehensions.
For consistency, I believe that the documentation for Dicts and Sets should mention comprehensions.
bitdancer commentedon Mar 16, 2015
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.
BreamoreBoy commentedon Mar 17, 2015
That was embarrassing, hopefully this is rather better.
frankmillman commentedon Mar 18, 2015
Lists and tuples are described like this -
I think a similar approach to Dicts and Sets could make sense -
Add a new example -
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).
vadmium commentedon Jan 3, 2016
The distiction about non-empty sets is a bit misleading. You can create an empty set via comprehension:
>>> {x for x in ()} set()MojoVampire commentedon Jan 5, 2016
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.
csabella commentedon Feb 24, 2019
Assigning to @Mariatta for the CPython mentored sprint.
furkanonder commentedon May 10, 2020
PR has been sent.