@@ -4823,7 +4823,13 @@ can be used interchangeably to index the same dictionary entry.
4823
4823
being added is already present, the value from the keyword argument
4824
4824
replaces the value from the positional argument.
4825
4825
4826
- To illustrate, the following examples all return a dictionary equal to
4826
+ Providing keyword arguments as in the first example only works for keys that
4827
+ are valid Python identifiers. Otherwise, any valid keys can be used.
4828
+
4829
+ Dictionaries compare equal if and only if they have the same ``(key,
4830
+ value) `` pairs (regardless of ordering). Order comparisons ('<', '<=', '>=', '>') raise
4831
+ :exc: `TypeError `. To illustrate dictionary creation and equality,
4832
+ the following examples all return a dictionary equal to
4827
4833
``{"one": 1, "two": 2, "three": 3} ``::
4828
4834
4829
4835
>>> a = dict(one=1, two=2, three=3)
@@ -4838,6 +4844,27 @@ can be used interchangeably to index the same dictionary entry.
4838
4844
Providing keyword arguments as in the first example only works for keys that
4839
4845
are valid Python identifiers. Otherwise, any valid keys can be used.
4840
4846
4847
+ Dictionaries preserve insertion order. Note that updating a key does not
4848
+ affect the order. Keys added after deletion are inserted at the end. ::
4849
+
4850
+ >>> d = {"one": 1, "two": 2, "three": 3, "four": 4}
4851
+ >>> d
4852
+ {'one': 1, 'two': 2, 'three': 3, 'four': 4}
4853
+ >>> list(d)
4854
+ ['one', 'two', 'three', 'four']
4855
+ >>> list(d.values())
4856
+ [1, 2, 3, 4]
4857
+ >>> d["one"] = 42
4858
+ >>> d
4859
+ {'one': 42, 'two': 2, 'three': 3, 'four': 4}
4860
+ >>> del d["two"]
4861
+ >>> d["two"] = None
4862
+ >>> d
4863
+ {'one': 42, 'three': 3, 'four': 4, 'two': None}
4864
+
4865
+ .. versionchanged :: 3.7
4866
+ Dictionary order is guaranteed to be insertion order. This behavior was
4867
+ an implementation detail of CPython from 3.6.
4841
4868
4842
4869
These are the operations that dictionaries support (and therefore, custom
4843
4870
mapping types should support too):
@@ -5008,32 +5035,6 @@ can be used interchangeably to index the same dictionary entry.
5008
5035
5009
5036
.. versionadded :: 3.9
5010
5037
5011
- Dictionaries compare equal if and only if they have the same ``(key,
5012
- value) `` pairs (regardless of ordering). Order comparisons ('<', '<=', '>=', '>') raise
5013
- :exc: `TypeError `.
5014
-
5015
- Dictionaries preserve insertion order. Note that updating a key does not
5016
- affect the order. Keys added after deletion are inserted at the end. ::
5017
-
5018
- >>> d = {"one": 1, "two": 2, "three": 3, "four": 4}
5019
- >>> d
5020
- {'one': 1, 'two': 2, 'three': 3, 'four': 4}
5021
- >>> list(d)
5022
- ['one', 'two', 'three', 'four']
5023
- >>> list(d.values())
5024
- [1, 2, 3, 4]
5025
- >>> d["one"] = 42
5026
- >>> d
5027
- {'one': 42, 'two': 2, 'three': 3, 'four': 4}
5028
- >>> del d["two"]
5029
- >>> d["two"] = None
5030
- >>> d
5031
- {'one': 42, 'three': 3, 'four': 4, 'two': None}
5032
-
5033
- .. versionchanged :: 3.7
5034
- Dictionary order is guaranteed to be insertion order. This behavior was
5035
- an implementation detail of CPython from 3.6.
5036
-
5037
5038
Dictionaries and dictionary views are reversible. ::
5038
5039
5039
5040
>>> d = {"one": 1, "two": 2, "three": 3, "four": 4}
0 commit comments