Skip to content

[3.9] bpo-29981: Add examples and update index for set, dict, and generator comprehensions'(GH-20272) #22836

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

Merged
merged 1 commit into from
Oct 25, 2020
Merged
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 @@ -308,6 +308,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 elements in an iterable and
return a dictionary with the results. ``results = {n: n ** 2 for n in
range(10)}`` generates a dictionary containing key ``n`` 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 @@ -1026,6 +1032,12 @@ Glossary
interface can be registered explicitly using
:func:`~abc.register`.

set comprehension
A compact way to process all or part of the elements in an iterable and
return a set with the results. ``results = {c for c in 'abracadabra' if
c not in 'abc'}`` generates the set of strings ``{'r', 'd'}``. See
:ref:`comprehensions`.

single dispatch
A form of :term:`generic function` dispatch where the implementation is
chosen based on the type of a single argument.
Expand Down
14 changes: 14 additions & 0 deletions Doc/library/stdtypes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4119,6 +4119,12 @@ The constructors for both classes work the same:
objects. If *iterable* is not specified, a new empty set is
returned.

Sets can be created by several means:

* Use a comma-separated list of elements within braces: ``{'jack', 'sjoerd'}``
* Use a set comprehension: ``{c for c in 'abracadabra' if c not in 'abc'}``
* Use the type constructor: ``set()``, ``set('foobar')``, ``set(['a', 'b', 'foo'])``

Instances of :class:`set` and :class:`frozenset` provide the following
operations:

Expand Down Expand Up @@ -4311,6 +4317,14 @@ pairs within braces, for example: ``{'jack': 4098, 'sjoerd': 4127}`` or ``{4098:
Return a new dictionary initialized from an optional positional argument
and a possibly empty set of keyword arguments.

Dictionaries can be created by several means:

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

If no positional argument is given, an empty dictionary is created.
If a positional argument is given and it is a mapping object, a dictionary
is created with the same key-value pairs as the mapping object. Otherwise,
Expand Down
4 changes: 4 additions & 0 deletions Doc/reference/expressions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,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 @@ -260,6 +262,7 @@ Set displays

.. index::
pair: set; display
pair: set; comprehensions
object: set
single: {} (curly brackets); set expression
single: , (comma); expression list
Expand Down Expand Up @@ -287,6 +290,7 @@ Dictionary displays

.. index::
pair: dictionary; display
pair: dictionary; comprehensions
key, datum, key/datum pair
object: dictionary
single: {} (curly brackets); dictionary expression
Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ Brian Curtin
Jason Curtis
Hakan Celik
Paul Dagnelie
Florian Dahlitz
Lisandro Dalcin
Darren Dale
Andrew Dalke
Expand Down