# Is order well-defined for multiple iterations over \`Dict\`?

**URL:** https://discourse.julialang.org/t/is-order-well-defined-for-multiple-iterations-over-dict/2180
**Category:** General Usage
**Tags:** question
**Created:** [February 20, 2017, 10:58am UTC](https://discourse.julialang.org/t/is-order-well-defined-for-multiple-iterations-over-dict/2180 "2017-02-20T10:58:32Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [February 20, 2017, 10:58am UTC](https://discourse.julialang.org/t/is-order-well-defined-for-multiple-iterations-over-dict/2180/1 "2017-02-20T10:58:32Z")

</div>

If I iterate over `(key,value)` pairs in `Dict`, or `keys(dict)`, or `values(dict)`, using _multiple iterations_, but _not modifying the dict between iterations_, is it guaranteed that I get the objects in the same order? (which is “random”, but I don’t care as long as it is consistent).

Looking at the implementation, this seems to be so, but I don’t know if this is something I can rely on (could not find it in the documentation).

---

<div class="post-metadata">

### Author: ![kevin.squire](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevin.squire/32/62_2.png) [@kevin.squire](https://discourse.julialang.org/u/kevin.squire)
#### Post date: [February 20, 2017, 2:50pm UTC](https://discourse.julialang.org/t/is-order-well-defined-for-multiple-iterations-over-dict/2180/2 "2017-02-20T14:50:16Z")

</div>

Yes. The underlying storage arrays are only resized/rehashed when the contents of the Dict itself are modified.

Cheers!  
Kevin

---

<div class="post-metadata">

### Author: ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)
#### Post date: [February 20, 2017, 6:15pm UTC](https://discourse.julialang.org/t/is-order-well-defined-for-multiple-iterations-over-dict/2180/3 "2017-02-20T18:15:57Z")

</div>

But that’s actually an implementation detail of `Dict`, and not necessarily guaranteed in the future, IIUC.  
But see [https://github.com/JuliaLang/julia/issues/20678](https://github.com/JuliaLang/julia/issues/20678)

---

<div class="post-metadata">

### Author: ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)
#### Post date: [February 20, 2017, 8:38pm UTC](https://discourse.julialang.org/t/is-order-well-defined-for-multiple-iterations-over-dict/2180/4 "2017-02-20T20:38:35Z")

</div>

Probably the best method (at least semantically) would be to convert the Dict into an OrderedDict (from DataStructures), this would freeze the order for later usage.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [February 21, 2017, 8:59am UTC](https://discourse.julialang.org/t/is-order-well-defined-for-multiple-iterations-over-dict/2180/5 "2017-02-21T08:59:22Z")

</div>

That’s what I thought. It would be nice to have that guarantee though, perhaps as a trait for some types.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [February 21, 2017, 9:00am UTC](https://discourse.julialang.org/t/is-order-well-defined-for-multiple-iterations-over-dict/2180/6 "2017-02-21T09:00:26Z")

</div>

Since I am only using it a few times, and always linearly after I finished accumulating values, it turns out that collecting and working on that is the fastest solution.
