Skip to content

Commit fffbe5c

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 a76b887 commit fffbe5c

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
@@ -86,8 +86,8 @@ Return an iterator over all keys in a dictionary.
8686
When the keys are stored internally in a hash table,
8787
as is the case for `Dict`,
8888
the order in which they are returned may vary.
89-
But `keys(a)` and `values(a)` both iterate `a` and
90-
return the elements in the same order.
89+
But `keys(a)`, `values(a)` and `pairs(a)` all iterate `a`
90+
and return the elements in the same order.
9191
9292
# Examples
9393
```jldoctest
@@ -112,8 +112,8 @@ Return an iterator over all values in a collection.
112112
When the values are stored internally in a hash table,
113113
as is the case for `Dict`,
114114
the order in which they are returned may vary.
115-
But `keys(a)` and `values(a)` both iterate `a` and
116-
return the elements in the same order.
115+
But `keys(a)`, `values(a)` and `pairs(a)` all iterate `a`
116+
and return the elements in the same order.
117117
118118
# Examples
119119
```jldoctest
@@ -136,6 +136,10 @@ values(a::AbstractDict) = ValueIterator(a)
136136
Return an iterator over `key => value` pairs for any
137137
collection that maps a set of keys to a set of values.
138138
This includes arrays, where the keys are the array indices.
139+
When the entries are stored internally in a hash table,
140+
as is the case for `Dict`, the order in which they are returned may vary.
141+
But `keys(a)`, `values(a)` and `pairs(a)` all iterate `a`
142+
and return the elements in the same order.
139143
140144
# Examples
141145
```jldoctest

test/dict.jl

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

766+
@testset "consistency of dict iteration order (issue #56841)" begin
767+
dict = Dict(randn() => randn() for _ = 1:100)
768+
@test all(zip(dict, keys(dict), values(dict), pairs(dict))) do (d, k, v, p)
769+
d == p && first(d) == first(p) == k && last(d) == last(p) == v
770+
end
771+
end
772+
766773
@testset "generators, similar" begin
767774
d = Dict(:a=>"a")
768775
# TODO: restore when 0.7 deprecation is removed

0 commit comments

Comments
 (0)