Skip to content

Commit 2d55aa9

Browse files
DahlitzFlorianRémi Lapeyre
andauthored
bpo-29981: Add examples and update index for set, dict, and generator comprehensions'(GH-20272)
Co-authored-by: Rémi Lapeyre <[email protected]>
1 parent 5ab27cc commit 2d55aa9

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
@@ -308,6 +308,12 @@ Glossary
308308
keys can be any object with :meth:`__hash__` and :meth:`__eq__` methods.
309309
Called a hash in Perl.
310310

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

1035+
set comprehension
1036+
A compact way to process all or part of the elements in an iterable and
1037+
return a set with the results. ``results = {c for c in 'abracadabra' if
1038+
c not in 'abc'}`` generates the set of strings ``{'r', 'd'}``. See
1039+
:ref:`comprehensions`.
1040+
10291041
single dispatch
10301042
A form of :term:`generic function` dispatch where the implementation is
10311043
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
@@ -4140,6 +4140,12 @@ The constructors for both classes work the same:
41404140
objects. If *iterable* is not specified, a new empty set is
41414141
returned.
41424142

4143+
Sets can be created by several means:
4144+
4145+
* Use a comma-separated list of elements within braces: ``{'jack', 'sjoerd'}``
4146+
* Use a set comprehension: ``{c for c in 'abracadabra' if c not in 'abc'}``
4147+
* Use the type constructor: ``set()``, ``set('foobar')``, ``set(['a', 'b', 'foo'])``
4148+
41434149
Instances of :class:`set` and :class:`frozenset` provide the following
41444150
operations:
41454151

@@ -4332,6 +4338,14 @@ pairs within braces, for example: ``{'jack': 4098, 'sjoerd': 4127}`` or ``{4098:
43324338
Return a new dictionary initialized from an optional positional argument
43334339
and a possibly empty set of keyword arguments.
43344340

4341+
Dictionaries can be created by several means:
4342+
4343+
* Use a comma-separated list of ``key: value`` pairs within braces:
4344+
``{'jack': 4098, 'sjoerd': 4127}`` or ``{4098: 'jack', 4127: 'sjoerd'}``
4345+
* Use a dict comprehension: ``{}``, ``{x: x ** 2 for x in range(10)}``
4346+
* Use the type constructor: ``dict()``,
4347+
``dict([('foo', 100), ('bar', 200)])``, ``dict(foo=100, bar=200)``
4348+
43354349
If no positional argument is given, an empty dictionary is created.
43364350
If a positional argument is given and it is a mapping object, a dictionary
43374351
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
@@ -382,6 +382,7 @@ Brian Curtin
382382
Jason Curtis
383383
Hakan Celik
384384
Paul Dagnelie
385+
Florian Dahlitz
385386
Lisandro Dalcin
386387
Darren Dale
387388
Andrew Dalke

0 commit comments

Comments
 (0)