Skip to content

bpo-29981: Update Index for set, dict, and generator 'comprehensions' #995

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Doc/glossary.rst
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,12 @@ Glossary
keys can be any object with :meth:`__hash__` and :meth:`__eq__` methods.
Called a hash in Perl.

dictionary comprehension
A compact way to process all or part of the items in a sequence
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A list/set/dict comprehension processes the items in an iterable.
If the entry for listcomp says 'sequence', it should be changed. I also do not like the 'all or part of' phrase. The iteration runs to completion. If one wants to not run the iteration to completion, one must use an explicit for loop. Perhaps the 'or part' refers to 'if' clauses. But that is a form of processing. If the iterable yields volatile items, they are gone.

and return a dictionary with the results. ``results = {n: n ** 2 for n in
range(10)}`` generates a dictionary containing key ``n`` which mapped to
value ``n ** 2``. See :ref:`comprehensions`.

dictionary view
The objects returned from :meth:`dict.keys`, :meth:`dict.values`, and
:meth:`dict.items` are called dictionary views. They provide a dynamic
Expand Down Expand Up @@ -921,6 +927,12 @@ Glossary
reserved for rare cases where there are large numbers of instances in a
memory-critical application.

set comprehension
A compact way to process all or part of the elements in a sequence and
return a set with the results. ``results = {c for c in 'abracadabra' if
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto

c not in 'abc'}`` generates the set of strings with ``{'r', 'd'}``. See
:ref:`comprehensions`.

sequence
An :term:`iterable` which supports efficient element access using integer
indices via the :meth:`__getitem__` special method and defines a
Expand Down
22 changes: 14 additions & 8 deletions Doc/library/stdtypes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3826,15 +3826,17 @@ another set. The :class:`frozenset` type is immutable and :term:`hashable` ---
its contents cannot be altered after it is created; it can therefore be used as
a dictionary key or as an element of another set.

Non-empty sets (not frozensets) can be created by placing a comma-separated list
of elements within braces, for example: ``{'jack', 'sjoerd'}``, in addition to the
:class:`set` constructor.

The constructors for both classes work the same:

.. class:: set([iterable])
frozenset([iterable])

Sets can be created by several ways:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'by several ways' is not idiomatic. Either 'by several means' or 'in several ways'. The latter is used below for dicts.


* Using a comma-separated list of elements within braces: ``{'jack', 'sjoerd'}``
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would use 'Use' instead of 'Using' here and rest of list

* Using a set comprehension: ``{c for c in 'abracadabra' if c not in 'abc'}``
* Using the type constructor: ``set()``, ``set('foobar')``, ``set(['a', 'b', 'foo'])``
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if we should still advertise the use of set([...]). We replaced all instances of it with set literals in the stdlib. See http://bugs.python.org/issue22823 for details.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO these examples are not for creating set literals. They demonstrate creating sets from iterables using the type constructor. Without these examples the user can though that {x for x in iterable} is the only way.


Return a new set or frozenset object whose elements are taken from
*iterable*. The elements of a set must be :term:`hashable`. To
represent sets of sets, the inner sets must be :class:`frozenset`
Expand Down Expand Up @@ -4024,14 +4026,18 @@ then they can be used interchangeably to index the same dictionary entry. (Note
however, that since computers store floating-point numbers as approximations it
is usually unwise to use them as dictionary keys.)

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 :class:`dict` constructor.

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

Dictionaries can be created in several ways:

* Using a comma-separated list of ``key: value`` pairs within braces:
``{'jack': 4098, 'sjoerd': 4127}`` or ``{4098: 'jack', 4127: 'sjoerd'}``
* Using a dict comprehension: ``{}``, ``{x: x ** 2 for x in range(10)}``
* Using the type constructor: ``dict()``,
``dict([('foo', 100), ('bar', 200)])``, ``dict(foo=100, bar=200)``

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

Expand Down
22 changes: 15 additions & 7 deletions Doc/reference/expressions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ ambiguities and allow common typos to pass uncaught.
Displays for lists, sets and dictionaries
-----------------------------------------

.. index:: single: comprehensions

For constructing a list, a set or a dictionary Python provides special syntax
called "displays", each of them in two flavors:

Expand Down Expand Up @@ -227,8 +229,10 @@ the list is constructed from the elements resulting from the comprehension.
Set displays
------------

.. index:: pair: set; display
object: set
.. index::
pair: set; display
pair: set; comprehensions
object: set

A set display is denoted by curly braces and distinguishable from dictionary
displays by the lack of colons separating keys and values:
Expand All @@ -251,9 +255,11 @@ dictionary.
Dictionary displays
-------------------

.. index:: pair: dictionary; display
key, datum, key/datum pair
object: dictionary
.. index::
pair: dictionary; display
pair: dictionary; comprehensions
key, datum, key/datum pair
object: dictionary

A dictionary display is a possibly empty series of key/datum pairs enclosed in
curly braces:
Expand Down Expand Up @@ -302,8 +308,10 @@ prevails.
Generator expressions
---------------------

.. index:: pair: generator; expression
object: generator
.. index::
pair: generator; expression
pair: generator; comprehensions
object: generator

A generator expression is a compact generator notation in parentheses:

Expand Down