# Do you have a clean way of looping through an array?

**URL:** <https://discourse.julialang.org/t/do-you-have-a-clean-way-of-looping-through-an-array/61788>\
**Category:** General Usage\
**Created:** [May 25, 2021, 3:19pm UTC](https://discourse.julialang.org/t/do-you-have-a-clean-way-of-looping-through-an-array/61788 "2021-05-25T15:19:49Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [May 25, 2021, 3:19pm UTC](https://discourse.julialang.org/t/do-you-have-a-clean-way-of-looping-through-an-array/61788/1 "2021-05-25T15:19:49Z")

</div>

Post it here. And when I say clean, it doesn’t mean clever. (I usually find that the clever ways of doing things are not the most legible.)

---

<div class="post-metadata">

**Author:** ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)\
**Post date:** [May 25, 2021, 3:26pm UTC](https://discourse.julialang.org/t/do-you-have-a-clean-way-of-looping-through-an-array/61788/2 "2021-05-25T15:26:41Z")

</div>

Could you clarify what kind of an array?

```julia
a = Array{Float64,3}(undef,3,3,3)
for i in eachindex(a)
    element = a[i]
end

```

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [May 25, 2021, 3:27pm UTC](https://discourse.julialang.org/t/do-you-have-a-clean-way-of-looping-through-an-array/61788/3 "2021-05-25T15:27:10Z")

</div>

I try to follow the manual when possible:

[https://docs.julialang.org/en/v1/manual/arrays/#Iteration](https://docs.julialang.org/en/v1/manual/arrays/#Iteration)

Some would also recommend `pairs` which returns the index and the value.

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [May 25, 2021, 3:28pm UTC](https://discourse.julialang.org/t/do-you-have-a-clean-way-of-looping-through-an-array/61788/4 "2021-05-25T15:28:41Z")

</div>

I like `eachindex` too!

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [May 25, 2021, 3:30pm UTC](https://discourse.julialang.org/t/do-you-have-a-clean-way-of-looping-through-an-array/61788/5 "2021-05-25T15:30:55Z")

</div>

`pairs` seems to be more robust than `enumerate`. That is my impression.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [May 25, 2021, 3:38pm UTC](https://discourse.julialang.org/t/do-you-have-a-clean-way-of-looping-through-an-array/61788/6 "2021-05-25T15:38:24Z")

</div>

This has been discussed here: [Any shortcut to 1:length(myVector)?](https://discourse.julialang.org/t/any-shortcut-to-1-length-myvector/52816)

---

<div class="post-metadata">

**Author:** ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)\
**Post date:** [May 25, 2021, 3:38pm UTC](https://discourse.julialang.org/t/do-you-have-a-clean-way-of-looping-through-an-array/61788/7 "2021-05-25T15:38:25Z")

</div>

I never used `pairs`, but it seems it is a bit more flexible in regards to the index. `enumerate` always uses a one based linear index.
