]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-29981: Add examples and update index for set, dict, and generator comprehensions...
authorMiss Skeleton (bot) <31488909+miss-islington@users.noreply.github.com>
Sun, 25 Oct 2020 02:53:18 +0000 (19:53 -0700)
committerGitHub <noreply@github.com>
Sun, 25 Oct 2020 02:53:18 +0000 (19:53 -0700)
Co-authored-by: Rémi Lapeyre <remi.lapeyre@henki.fr>
(cherry picked from commit 2d55aa9e37c9c84f4f6a8135d0326da0bcd8f38b)

Co-authored-by: Florian Dahlitz <f2dahlitz@freenet.de>
Doc/glossary.rst
Doc/library/stdtypes.rst
Doc/reference/expressions.rst
Misc/ACKS

index 632ed3f4301c0745201785575b7bec9710172f2e..d94fc4469c50af0d5a6a535022afc5db30f9b9d9 100644 (file)
@@ -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
@@ -1026,6 +1032,12 @@ Glossary
       interface can be registered explicitly using
       :func:`~abc.ABCMeta.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.
index ccd2f99ccb34f6acf2afd44f2b63af36b6c4d476..78c7cc76d7e7bb9204d17e789ea15231fe1786f0 100644 (file)
@@ -3994,6 +3994,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:
 
@@ -4186,6 +4192,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,
index b68c29860cf3329ccd8020293ab60754eb45c37f..81dd6fc860355fbab8e004794fb381db6d14b44e 100644 (file)
@@ -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:
 
@@ -260,6 +262,7 @@ Set displays
 
 .. index::
    pair: set; display
+   pair: set; comprehensions
    object: set
    single: {} (curly brackets); set expression
    single: , (comma); expression list
@@ -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
index b0246402c354df463484dd909b7c84faef06de96..d44c794e425e09eaa222a7c27c7ec22d888476a8 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -368,6 +368,7 @@ Brian Curtin
 Hakan Celik
 Jason Curtis
 Paul Dagnelie
+Florian Dahlitz 
 Lisandro Dalcin
 Darren Dale
 Andrew Dalke