# Enumerate(dict) behavior

**URL:** <https://discourse.julialang.org/t/enumerate-dict-behavior/5312>\
**Category:** Data\
**Created:** [August 9, 2017, 8:51pm UTC](https://discourse.julialang.org/t/enumerate-dict-behavior/5312 "2017-08-09T20:51:45Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![sambitdash](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sambitdash/32/1377_2.png) [@sambitdash](https://discourse.julialang.org/u/sambitdash)\
**Post date:** [August 9, 2017, 8:51pm UTC](https://discourse.julialang.org/t/enumerate-dict-behavior/5312/1 "2017-08-09T20:51:45Z")

</div>

Hi All,

`enumerate(iter)` behavior enumerates over an index and dataItem to the associated index. For associative containers like Dict one would believe the natural iteration will be over named indexes which is the `(key, value)`. However, we see a iterator over an `Int` and `Pair{K,V}`.

Any reason why this has been the approach? Will this behavior change in future?

regards,

Sambit

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [August 9, 2017, 8:59pm UTC](https://discourse.julialang.org/t/enumerate-dict-behavior/5312/2 "2017-08-09T20:59:14Z")

</div>

> [@sambitdash](#):
>
> enumerates over an index and dataItem to the associated index.

No it doesn’t. Please read the doc of it.

```julia
help?> enumerate
search: enumerate Enumerate

  enumerate(iter)

  An iterator that yields (i, x) where i is a counter starting at 1, and x is the ith value from
  the given iterator. It's useful when you need not only the values x over which you are

```

> [@sambitdash](#):
>
> Will this behavior change in future?

So no.

---

<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:** [August 10, 2017, 1:44pm UTC](https://discourse.julialang.org/t/enumerate-dict-behavior/5312/3 "2017-08-10T13:44:27Z")

</div>

If d is a Dict, you can just do

for (k, v) in d  
println(k, v)  
end

to iterate over keys and values.

---

<div class="post-metadata">

**Author:** ![sambitdash](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sambitdash/32/1377_2.png) [@sambitdash](https://discourse.julialang.org/u/sambitdash)\
**Post date:** [August 10, 2017, 1:47pm UTC](https://discourse.julialang.org/t/enumerate-dict-behavior/5312/4 "2017-08-10T13:47:13Z")

</div>

This definitely makes sense. Thanks.

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [October 10, 2017, 8:24pm UTC](https://discourse.julialang.org/t/enumerate-dict-behavior/5312/5 "2017-10-10T20:24:03Z")

</div>

This makes me wonder why the syntax for iterating over key-value pairs hasn’t been generalized to other data formats, such as custom types or array of arrays. For example:

m = [rand(10) for i in 1:10]

for (i,v) in m  
#do some stuff  
end

---

<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:** [October 10, 2017, 10:25pm UTC](https://discourse.julialang.org/t/enumerate-dict-behavior/5312/6 "2017-10-10T22:25:33Z")

</div>

I’m not sure what you would want `i` and `v` to be in that case?

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [October 10, 2017, 10:48pm UTC](https://discourse.julialang.org/t/enumerate-dict-behavior/5312/7 "2017-10-10T22:48:10Z")

</div>

Presumably this would be similar to `for (i, v) in enumerate(m)`. It would seem pretty surprising for iterating a vector of values not to yield those values.

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [October 10, 2017, 10:58pm UTC](https://discourse.julialang.org/t/enumerate-dict-behavior/5312/8 "2017-10-10T22:58:58Z")

</div>

Sorry for being unclear. As @StefanKarpinski said, I was thinking that it would function just like `enumerate()`. So i would be an index and v would be a sub-array. It would have the benefit of being consistent with the syntax of the dictionary iterator and would also be more concise than `enummerate()`.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [October 10, 2017, 11:01pm UTC](https://discourse.julialang.org/t/enumerate-dict-behavior/5312/9 "2017-10-10T23:01:43Z")

</div>

Unlike dict iteration which is undefined or does different things than iterate key-value pairs in various languages (e.g. in Python, iterating a dict yields the keys), I don’t think there’s any language where iterating a vector of values does anything besides returning those values in order. So that would a truly radical language change. On the other hand, changing dictionaries to yield values and not keys is currently being discussed as a way of conceptually unifying arrays and dictionaries:

[https://github.com/JuliaLang/julia/issues/24019](https://github.com/JuliaLang/julia/issues/24019)

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [October 11, 2017, 12:19am UTC](https://discourse.julialang.org/t/enumerate-dict-behavior/5312/10 "2017-10-11T00:19:39Z")

</div>

> [@StefanKarpinski](#):
>
> I don’t think there’s any language where iterating a vector of values does anything besides returning those values in order

I’m 99% sure that iterating an array in JS returns the indices, which I always find consistently annoy.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [October 11, 2017, 3:12am UTC](https://discourse.julialang.org/t/enumerate-dict-behavior/5312/11 "2017-10-11T03:12:00Z")

</div>

It’s also worth noting that v0.7 has the new `pairs` function, allowing you to iterate dictionaries and arrays alike:

```julia
for (i, value) in pairs(x)
    @assert x[i] == value
end

```
