@@ -4838,6 +4838,28 @@ can be used interchangeably to index the same dictionary entry.
4838
4838
Providing keyword arguments as in the first example only works for keys that
4839
4839
are valid Python identifiers. Otherwise, any valid keys can be used.
4840
4840
4841
+ Dictionaries preserve insertion order. Note that updating a key does not
4842
+ affect the order. Keys added after deletion are inserted at the end. ::
4843
+
4844
+ >>> d = {"one": 1, "two": 2, "three": 3, "four": 4}
4845
+ >>> d
4846
+ {'one': 1, 'two': 2, 'three': 3, 'four': 4}
4847
+ >>> list(d)
4848
+ ['one', 'two', 'three', 'four']
4849
+ >>> list(d.values())
4850
+ [1, 2, 3, 4]
4851
+ >>> d["one"] = 42
4852
+ >>> d
4853
+ {'one': 42, 'two': 2, 'three': 3, 'four': 4}
4854
+ >>> del d["two"]
4855
+ >>> d["two"] = None
4856
+ >>> d
4857
+ {'one': 42, 'three': 3, 'four': 4, 'two': None}
4858
+
4859
+ .. versionchanged :: 3.7
4860
+ Dictionary order is guaranteed to be insertion order. This behavior was
4861
+ an implementation detail of CPython from 3.6.
4862
+
4841
4863
4842
4864
These are the operations that dictionaries support (and therefore, custom
4843
4865
mapping types should support too):
@@ -5012,28 +5034,6 @@ can be used interchangeably to index the same dictionary entry.
5012
5034
value) `` pairs (regardless of ordering). Order comparisons ('<', '<=', '>=', '>') raise
5013
5035
:exc: `TypeError `.
5014
5036
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
5037
Dictionaries and dictionary views are reversible. ::
5038
5038
5039
5039
>>> d = {"one": 1, "two": 2, "three": 3, "four": 4}
0 commit comments