Skip to content

Commit 60bef61

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 eac21a0 commit 60bef61

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.ABCMeta.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
@@ -3994,6 +3994,12 @@ The constructors for both classes work the same:
39943994
objects. If *iterable* is not specified, a new empty set is
39953995
returned.
39963996

3997+
Sets can be created by several means:
3998+
3999+
* Use a comma-separated list of elements within braces: ``{'jack', 'sjoerd'}``
4000+
* Use a set comprehension: ``{c for c in 'abracadabra' if c not in 'abc'}``
4001+
* Use the type constructor: ``set()``, ``set('foobar')``, ``set(['a', 'b', 'foo'])``
4002+
39974003
Instances of :class:`set` and :class:`frozenset` provide the following
39984004
operations:
39994005

@@ -4186,6 +4192,14 @@ pairs within braces, for example: ``{'jack': 4098, 'sjoerd': 4127}`` or ``{4098:
41864192
Return a new dictionary initialized from an optional positional argument
41874193
and a possibly empty set of keyword arguments.
41884194

4195+
Dictionaries can be created by several means:
4196+
4197+
* Use a comma-separated list of ``key: value`` pairs within braces:
4198+
``{'jack': 4098, 'sjoerd': 4127}`` or ``{4098: 'jack', 4127: 'sjoerd'}``
4199+
* Use a dict comprehension: ``{}``, ``{x: x ** 2 for x in range(10)}``
4200+
* Use the type constructor: ``dict()``,
4201+
``dict([('foo', 100), ('bar', 200)])``, ``dict(foo=100, bar=200)``
4202+
41894203
If no positional argument is given, an empty dictionary is created.
41904204
If a positional argument is given and it is a mapping object, a dictionary
41914205
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
@@ -368,6 +368,7 @@ Brian Curtin
368368
Hakan Celik
369369
Jason Curtis
370370
Paul Dagnelie
371+
Florian Dahlitz
371372
Lisandro Dalcin
372373
Darren Dale
373374
Andrew Dalke

0 commit comments

Comments
 (0)