Skip to content

Mention dict and set comps in library reference #67865

Closed as duplicate
Closed as duplicate

Description

@frankmillman
BPO 23677
Nosy @ezio-melotti, @bitdancer, @vadmium, @MojoVampire, @Mariatta, @csabella, @furkanonder
PRs
  • bpo-23677: Mention dict and set comprehension in stdtypes #20027
  • Files
  • issue23677.diff: minimal changes to stdtypes.rst
  • issue23677_v2.diff
  • 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 = None
    created_at = <Date 2015-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 = <Date 2020-05-10.18:23:31.452>
    user = 'https://bugs.python.org/FrankMillman'

    bugs.python.org fields:

    activity = <Date 2020-05-10.18:23:31.452>
    actor = 'furkanonder'
    assignee = 'Mariatta'
    closed = False
    closed_date = None
    closer = None
    components = ['Documentation']
    creation = <Date 2015-03-16.08:50:55.042>
    creator = 'FrankMillman'
    dependencies = []
    files = ['38507', '38534']
    hgrepos = []
    issue_num = 23677
    keywords = ['patch']
    message_count = 8.0
    messages = ['238186', '238203', '238362', '238380', '257427', '257514', '336486', '368590']
    nosy_count = 9.0
    nosy_names = ['FrankMillman', 'ezio.melotti', 'r.david.murray', 'docs@python', 'martin.panter', 'josh.r', 'Mariatta', 'cheryl.sabella', 'furkanonder']
    pr_nums = ['20027']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue23677'
    versions = ['Python 3.7', 'Python 3.8']

    Activity

    1. frankmillman commented on Mar 16, 2015

      frankmillmanmannequin
      MannequinAuthor

      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.

    2. bitdancer commented on Mar 16, 2015

      @bitdancer
      Member

      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.

    3. BreamoreBoy commented on Mar 17, 2015

      BreamoreBoymannequin
      Mannequin

      That was embarrassing, hopefully this is rather better.

    4. frankmillman commented on Mar 18, 2015

      frankmillmanmannequin
      MannequinAuthor

      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).

    5. vadmium commented on Jan 3, 2016

      @vadmium
      Member

      The distiction about non-empty sets is a bit misleading. You can create an empty set via comprehension:

      >>> {x for x in ()}
      set()
    6. MojoVampire commented on Jan 5, 2016

      MojoVampiremannequin
      Mannequin

      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.

    7. csabella commented on Feb 24, 2019

      @csabella
      Contributor

      Assigning to @Mariatta for the CPython mentored sprint.

    8. furkanonder commented on May 10, 2020

      furkanondermannequin
      Mannequin

      PR has been sent.

    9. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    Labels

    3.7 (EOL)end of life3.8 (EOL)end of lifedocsDocumentation in the Doc dirtype-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions