Skip to content

Commit aa276c3

Browse files
cossioKristofferC
authored andcommitted
dict docs: document that ordering of keys/values/pairs match iterate (#56842)
Fix #56841. Currently the documentation states that keys(dict) and values(dict) iterate in the same order. But it is not stated whether this is the same order as that used by pairs(dict), or when looping, for (k,v) in dict. This PR makes this guarantee explicit. (cherry picked from commit 796d823)
1 parent 6f6bc95 commit aa276c3

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

base/abstractdict.jl

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ Return an iterator over all keys in a dictionary.
8888
When the keys are stored internally in a hash table,
8989
as is the case for `Dict`,
9090
the order in which they are returned may vary.
91-
But `keys(a)` and `values(a)` both iterate `a` and
92-
return the elements in the same order.
91+
But `keys(a)`, `values(a)` and `pairs(a)` all iterate `a`
92+
and return the elements in the same order.
9393
9494
# Examples
9595
```jldoctest
@@ -114,8 +114,8 @@ Return an iterator over all values in a collection.
114114
When the values are stored internally in a hash table,
115115
as is the case for `Dict`,
116116
the order in which they are returned may vary.
117-
But `keys(a)` and `values(a)` both iterate `a` and
118-
return the elements in the same order.
117+
But `keys(a)`, `values(a)` and `pairs(a)` all iterate `a`
118+
and return the elements in the same order.
119119
120120
# Examples
121121
```jldoctest
@@ -138,6 +138,10 @@ values(a::AbstractDict) = ValueIterator(a)
138138
Return an iterator over `key => value` pairs for any
139139
collection that maps a set of keys to a set of values.
140140
This includes arrays, where the keys are the array indices.
141+
When the entries are stored internally in a hash table,
142+
as is the case for `Dict`, the order in which they are returned may vary.
143+
But `keys(a)`, `values(a)` and `pairs(a)` all iterate `a`
144+
and return the elements in the same order.
141145
142146
# Examples
143147
```jldoctest

test/dict.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,13 @@ end
787787
[v for (k, v) in d] == [d[x[1]] for (i, x) in enumerate(d)]
788788
end
789789

790+
@testset "consistency of dict iteration order (issue #56841)" begin
791+
dict = Dict(randn() => randn() for _ = 1:100)
792+
@test all(zip(dict, keys(dict), values(dict), pairs(dict))) do (d, k, v, p)
793+
d == p && first(d) == first(p) == k && last(d) == last(p) == v
794+
end
795+
end
796+
790797
@testset "generators, similar" begin
791798
d = Dict(:a=>"a")
792799
# TODO: restore when 0.7 deprecation is removed

0 commit comments

Comments
 (0)