Skip to content

Commit 2cccc29

Browse files
bpo-29981: Add examples and update index for set, dict, and generator comprehensions'(GH-20272)
Co-authored-by: Rémi Lapeyre <[email protected]> (cherry picked from commit 2d55aa9) Co-authored-by: Florian Dahlitz <[email protected]>
1 parent 427cb0a commit 2cccc29

File tree

4 files changed

+31
-0
lines changed

4 files changed

+31
-0
lines changed

Doc/glossary.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,12 @@ Glossary
309309
keys can be any object with :meth:`__hash__` and :meth:`__eq__` methods.
310310
Called a hash in Perl.
311311

312+
dictionary comprehension
313+
A compact way to process all or part of the elements in an iterable and
314+
return a dictionary with the results. ``results = {n: n ** 2 for n in
315+
range(10)}`` generates a dictionary containing key ``n`` mapped to
316+
value ``n ** 2``. See :ref:`comprehensions`.
317+
312318
dictionary view
313319
The objects returned from :meth:`dict.keys`, :meth:`dict.values`, and
314320
:meth:`dict.items` are called dictionary views. They provide a dynamic
@@ -1027,6 +1033,12 @@ Glossary
10271033
interface can be registered explicitly using
10281034
:func:`~abc.ABCMeta.register`.
10291035

1036+
set comprehension
1037+
A compact way to process all or part of the elements in an iterable and
1038+
return a set with the results. ``results = {c for c in 'abracadabra' if
1039+
c not in 'abc'}`` generates the set of strings ``{'r', 'd'}``. See
1040+
:ref:`comprehensions`.
1041+
10301042
single dispatch
10311043
A form of :term:`generic function` dispatch where the implementation is
10321044
chosen based on the type of a single argument.

Doc/library/stdtypes.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4119,6 +4119,12 @@ The constructors for both classes work the same:
41194119
objects. If *iterable* is not specified, a new empty set is
41204120
returned.
41214121

4122+
Sets can be created by several means:
4123+
4124+
* Use a comma-separated list of elements within braces: ``{'jack', 'sjoerd'}``
4125+
* Use a set comprehension: ``{c for c in 'abracadabra' if c not in 'abc'}``
4126+
* Use the type constructor: ``set()``, ``set('foobar')``, ``set(['a', 'b', 'foo'])``
4127+
41224128
Instances of :class:`set` and :class:`frozenset` provide the following
41234129
operations:
41244130

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

4320+
Dictionaries can be created by several means:
4321+
4322+
* Use a comma-separated list of ``key: value`` pairs within braces:
4323+
``{'jack': 4098, 'sjoerd': 4127}`` or ``{4098: 'jack', 4127: 'sjoerd'}``
4324+
* Use a dict comprehension: ``{}``, ``{x: x ** 2 for x in range(10)}``
4325+
* Use the type constructor: ``dict()``,
4326+
``dict([('foo', 100), ('bar', 200)])``, ``dict(foo=100, bar=200)``
4327+
43144328
If no positional argument is given, an empty dictionary is created.
43154329
If a positional argument is given and it is a mapping object, a dictionary
43164330
is created with the same key-value pairs as the mapping object. Otherwise,

Doc/reference/expressions.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,8 @@ ambiguities and allow common typos to pass uncaught.
162162
Displays for lists, sets and dictionaries
163163
-----------------------------------------
164164

165+
.. index:: single: comprehensions
166+
165167
For constructing a list, a set or a dictionary Python provides special syntax
166168
called "displays", each of them in two flavors:
167169

@@ -260,6 +262,7 @@ Set displays
260262

261263
.. index::
262264
pair: set; display
265+
pair: set; comprehensions
263266
object: set
264267
single: {} (curly brackets); set expression
265268
single: , (comma); expression list
@@ -287,6 +290,7 @@ Dictionary displays
287290

288291
.. index::
289292
pair: dictionary; display
293+
pair: dictionary; comprehensions
290294
key, datum, key/datum pair
291295
object: dictionary
292296
single: {} (curly brackets); dictionary expression

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,7 @@ Brian Curtin
379379
Jason Curtis
380380
Hakan Celik
381381
Paul Dagnelie
382+
Florian Dahlitz
382383
Lisandro Dalcin
383384
Darren Dale
384385
Andrew Dalke

0 commit comments

Comments
 (0)