@@ -4629,7 +4629,13 @@ can be used interchangeably to index the same dictionary entry.
4629
4629
being added is already present, the value from the keyword argument
4630
4630
replaces the value from the positional argument.
4631
4631
4632
- To illustrate, the following examples all return a dictionary equal to
4632
+ Providing keyword arguments as in the first example only works for keys that
4633
+ are valid Python identifiers. Otherwise, any valid keys can be used.
4634
+
4635
+ Dictionaries compare equal if and only if they have the same ``(key,
4636
+ value) `` pairs (regardless of ordering). Order comparisons ('<', '<=', '>=', '>') raise
4637
+ :exc: `TypeError `. To illustrate dictionary creation and equality,
4638
+ the following examples all return a dictionary equal to
4633
4639
``{"one": 1, "two": 2, "three": 3} ``::
4634
4640
4635
4641
>>> a = dict(one=1, two=2, three=3)
@@ -4644,6 +4650,27 @@ can be used interchangeably to index the same dictionary entry.
4644
4650
Providing keyword arguments as in the first example only works for keys that
4645
4651
are valid Python identifiers. Otherwise, any valid keys can be used.
4646
4652
4653
+ Dictionaries preserve insertion order. Note that updating a key does not
4654
+ affect the order. Keys added after deletion are inserted at the end. ::
4655
+
4656
+ >>> d = {"one": 1, "two": 2, "three": 3, "four": 4}
4657
+ >>> d
4658
+ {'one': 1, 'two': 2, 'three': 3, 'four': 4}
4659
+ >>> list(d)
4660
+ ['one', 'two', 'three', 'four']
4661
+ >>> list(d.values())
4662
+ [1, 2, 3, 4]
4663
+ >>> d["one"] = 42
4664
+ >>> d
4665
+ {'one': 42, 'two': 2, 'three': 3, 'four': 4}
4666
+ >>> del d["two"]
4667
+ >>> d["two"] = None
4668
+ >>> d
4669
+ {'one': 42, 'three': 3, 'four': 4, 'two': None}
4670
+
4671
+ .. versionchanged :: 3.7
4672
+ Dictionary order is guaranteed to be insertion order. This behavior was
4673
+ an implementation detail of CPython from 3.6.
4647
4674
4648
4675
These are the operations that dictionaries support (and therefore, custom
4649
4676
mapping types should support too):
@@ -4814,32 +4841,6 @@ can be used interchangeably to index the same dictionary entry.
4814
4841
4815
4842
.. versionadded :: 3.9
4816
4843
4817
- Dictionaries compare equal if and only if they have the same ``(key,
4818
- value) `` pairs (regardless of ordering). Order comparisons ('<', '<=', '>=', '>') raise
4819
- :exc: `TypeError `.
4820
-
4821
- Dictionaries preserve insertion order. Note that updating a key does not
4822
- affect the order. Keys added after deletion are inserted at the end. ::
4823
-
4824
- >>> d = {"one": 1, "two": 2, "three": 3, "four": 4}
4825
- >>> d
4826
- {'one': 1, 'two': 2, 'three': 3, 'four': 4}
4827
- >>> list(d)
4828
- ['one', 'two', 'three', 'four']
4829
- >>> list(d.values())
4830
- [1, 2, 3, 4]
4831
- >>> d["one"] = 42
4832
- >>> d
4833
- {'one': 42, 'two': 2, 'three': 3, 'four': 4}
4834
- >>> del d["two"]
4835
- >>> d["two"] = None
4836
- >>> d
4837
- {'one': 42, 'three': 3, 'four': 4, 'two': None}
4838
-
4839
- .. versionchanged :: 3.7
4840
- Dictionary order is guaranteed to be insertion order. This behavior was
4841
- an implementation detail of CPython from 3.6.
4842
-
4843
4844
Dictionaries and dictionary views are reversible. ::
4844
4845
4845
4846
>>> d = {"one": 1, "two": 2, "three": 3, "four": 4}
0 commit comments