diff options
author | Florian Dahlitz <f2dahlitz@freenet.de> | 2020-10-20 21:27:07 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-10-20 21:27:07 (GMT) |
commit | 2d55aa9e37c9c84f4f6a8135d0326da0bcd8f38b (patch) | |
tree | 32b2fdc5e25397a54d3648fea8928e16cb9edf6b /Doc/library | |
parent | 5ab27cc518f614a0b954ff3eb125290f264242d5 (diff) | |
download | cpython-2d55aa9e37c9c84f4f6a8135d0326da0bcd8f38b.zip cpython-2d55aa9e37c9c84f4f6a8135d0326da0bcd8f38b.tar.gz cpython-2d55aa9e37c9c84f4f6a8135d0326da0bcd8f38b.tar.bz2 |
bpo-29981: Add examples and update index for set, dict, and generator comprehensions'(GH-20272)
Co-authored-by: RĂ©mi Lapeyre <remi.lapeyre@henki.fr>
Diffstat (limited to 'Doc/library')
-rw-r--r-- | Doc/library/stdtypes.rst | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst index 2fc7a61..c74d164 100644 --- a/Doc/library/stdtypes.rst +++ b/Doc/library/stdtypes.rst @@ -4140,6 +4140,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: @@ -4332,6 +4338,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, |