summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorNico Dinata <ncdnt@icloud.com>2018-12-21 05:42:35 +1100
committerDivay Prakash <divayprakash@users.noreply.github.com>2018-12-21 00:12:35 +0530
commit62926bd431868a0c8de3912b29d30dc11c196aee (patch)
tree2043898035024922eaeb7141f49eea405ac7a782
parentd5b09d5b13f2dcb29b812c6b55011ab8bab8d1be (diff)
[python3/en] Add note about dictionary item ordering in Python 3.7+ (#3423)
* Add changes to dict item ordering in Python 3.7+ * Fix line length * Fix typo in example
-rw-r--r--python3.html.markdown13
1 files changed, 8 insertions, 5 deletions
diff --git a/python3.html.markdown b/python3.html.markdown
index 4d5bb3ae..25f88943 100644
--- a/python3.html.markdown
+++ b/python3.html.markdown
@@ -314,16 +314,19 @@ valid_dict = {(1,2,3):[1,2,3]} # Values can be of any type, however.
filled_dict["one"] # => 1
# Get all keys as an iterable with "keys()". We need to wrap the call in list()
-# to turn it into a list. We'll talk about those later. Note - Dictionary key
-# ordering is not guaranteed. Your results might not match this exactly.
-list(filled_dict.keys()) # => ["three", "two", "one"]
+# to turn it into a list. We'll talk about those later. Note - for Python
+# versions <3.7, dictionary key ordering is not guaranteed. Your results might
+# not match the example below exactly. However, as of Python 3.7, dictionary
+# items maintain the order at which they are inserted into the dictionary.
+list(filled_dict.keys()) # => ["three", "two", "one"] in Python <3.7
+list(filled_dict.keys()) # => ["one", "two", "three"] in Python 3.7+
# Get all values as an iterable with "values()". Once again we need to wrap it
# in list() to get it out of the iterable. Note - Same as above regarding key
# ordering.
-list(filled_dict.values()) # => [3, 2, 1]
-
+list(filled_dict.values()) # => [3, 2, 1] in Python <3.7
+list(filled_dict.values()) # => [1, 2, 3] in Python 3.7+
# Check for existence of keys in a dictionary with "in"
"one" in filled_dict # => True