# Enumeratezip

**URL:** <https://discourse.julialang.org/t/enumeratezip/54617>\
**Category:** General Usage\
**Created:** [February 4, 2021, 1:09pm UTC](https://discourse.julialang.org/t/enumeratezip/54617 "2021-02-04T13:09:47Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![fgerick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fgerick/32/13228_2.png) [@fgerick](https://discourse.julialang.org/u/fgerick)\
**Post date:** [February 4, 2021, 1:09pm UTC](https://discourse.julialang.org/t/enumeratezip/54617/1 "2021-02-04T13:09:47Z")

</div>

I often would like to use something like `enumerate(a,b,c)` and get the functionality:

```julia
a,b,c = rand(10),rand(10),rand(10)
for (i,ai,bi,ci) in enumerate(a,b,c)
   @show i ai bi ci
end

```

something that could emulate this is `enumeratezip(a...) = zip(eachindex(first(a)), a...)` so that

```julia
a,b,c = rand(10),rand(10),rand(10)
for (i,ai,bi,ci) in enumeratezip(a,b,c)
   @show i ai bi ci
end

```

I don’t know if this can be problematic in some cases and maybe there’s a more elegant version to do the same thing without defining `enumeratezip` or writing the `zip` by hand. I would be interested to know! Maybe this feature is something that someone else came across wanting to have?

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [February 4, 2021, 1:30pm UTC](https://discourse.julialang.org/t/enumeratezip/54617/2 "2021-02-04T13:30:51Z")

</div>

What about

```julia
julia> for (i,(ai,bi,ci)) in enumerate(zip(a,b,c))
          @show i ai bi ci
          end
i = 1
ai = 0.9570998904436379
bi = 0.5775517184568557
ci = 0.3987638983159012
i = 2
ai = 0.9761040253755489
bi = 0.7214272853124479
ci = 0.7986223352162409
i = 3
ai = 0.9435897685169268
bi = 0.10027768357097111
ci = 0.41432976381125064
i = 4
...

```

?

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [February 4, 2021, 1:35pm UTC](https://discourse.julialang.org/t/enumeratezip/54617/3 "2021-02-04T13:35:41Z")

</div>

Looks like a decent idea to me, but you could run into a couple issues with just using `eachindex(a)` if `a`, `b`, and `c` aren’t the same…:

1. …shape: you could want to index into `b` and `c` with `i`, but `eachindex(a)` could return indices that only work for `a`. Simple to fix, just compute indices from `eachindex(b)` and `eachindex(c)` too.
2. …size: if `b` or `c` is smaller than `a`, the loop will terminate before you exhaust `eachindex(a)`. This isn’t necessarily a problem, but someone not looking too closely could think `a` was exhausted at the last iteration because that’s what you chose to `eachindex`. A comment or adding those indices from `b` and `c` would help make it clearer, I think.

---

<div class="post-metadata">

**Author:** ![fgerick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fgerick/32/13228_2.png) [@fgerick](https://discourse.julialang.org/u/fgerick)\
**Post date:** [February 4, 2021, 1:35pm UTC](https://discourse.julialang.org/t/enumeratezip/54617/4 "2021-02-04T13:35:55Z")

</div>

It’s not bad! But still I find it a little unnecessarily clumsy.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [February 4, 2021, 1:36pm UTC](https://discourse.julialang.org/t/enumeratezip/54617/5 "2021-02-04T13:36:54Z")

</div>

Really? I really like the elegance of composition, instead of having to make a custom function.

---

<div class="post-metadata">

**Author:** ![fgerick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fgerick/32/13228_2.png) [@fgerick](https://discourse.julialang.org/u/fgerick)\
**Post date:** [February 4, 2021, 1:37pm UTC](https://discourse.julialang.org/t/enumeratezip/54617/6 "2021-02-04T13:37:53Z")

</div>

oh no of course that’s much better! I guess I just would like to have `enumerate` to support splatting 🙂

---

<div class="post-metadata">

**Author:** ![fgerick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fgerick/32/13228_2.png) [@fgerick](https://discourse.julialang.org/u/fgerick)\
**Post date:** [February 4, 2021, 1:40pm UTC](https://discourse.julialang.org/t/enumeratezip/54617/7 "2021-02-04T13:40:27Z")

</div>

You can already use `zip(rand(10),rand(11))`, so something similar must be possible enumerating the whole thing?

---

<div class="post-metadata">

**Author:** ![simonschoelly](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simonschoelly/32/21606_2.png) [@simonschoelly](https://discourse.julialang.org/u/simonschoelly)\
**Post date:** [February 4, 2021, 1:42pm UTC](https://discourse.julialang.org/t/enumeratezip/54617/8 "2021-02-04T13:42:43Z")

</div>

Note that that `enumerate` does always start at 1, while `eachindex` might not. For example:

```julia
julia> using OffsetArrays: OffsetVector

julia> v = OffsetVector(['a', 'b', 'c'], 5)
3-element OffsetArray(::Vector{Char}, 6:8) with eltype Char with indices 6:8:
 'a': ASCII/Unicode U+0061 (category Ll: Letter, lowercase)
 'b': ASCII/Unicode U+0062 (category Ll: Letter, lowercase)
 'c': ASCII/Unicode U+0063 (category Ll: Letter, lowercase)

julia> enumerate(v) |> collect
3-element Vector{Tuple{Int64, Char}}:
 (1, 'a')
 (2, 'b')
 (3, 'c')

julia> zip(eachindex(v), v) |> collect
3-element OffsetArray(::Vector{Tuple{Int64, Char}}, 6:8) with eltype Tuple{Int64, Char} with indices 6:8:
 (6, 'a')
 (7, 'b')
 (8, 'c')

```

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [February 4, 2021, 1:49pm UTC](https://discourse.julialang.org/t/enumeratezip/54617/9 "2021-02-04T13:49:27Z")

</div>

Not really sure if there is a proper `enumerate` equivalent for multiple iterables. `enumerate( zip (a, b, c) )` wouldn’t work because whatever index you get is intended for the `zip` object, not `a,b,c`.  
`zip(eachindex(a), eachindex(b), a, b)` would be closer, but there must be a more elegant way to do that given `d = (a, b, c)`, similar to your `enumeratezip(a...)` call.

---

<div class="post-metadata">

**Author:** ![fgerick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fgerick/32/13228_2.png) [@fgerick](https://discourse.julialang.org/u/fgerick)\
**Post date:** [February 4, 2021, 4:23pm UTC](https://discourse.julialang.org/t/enumeratezip/54617/10 "2021-02-04T16:23:59Z")

</div>

My intention was actually not meant for the case of an `OffsetArray`. I guess the solution by @DNF is what I will go with. And after looking at it a bit longer I might get used to the double brackets, separating index and elements!
