# Bounds error while iterating over for loop

**URL:** <https://discourse.julialang.org/t/bounds-error-while-iterating-over-for-loop/87245>\
**Category:** General Usage\
**Created:** [September 14, 2022, 2:30am UTC](https://discourse.julialang.org/t/bounds-error-while-iterating-over-for-loop/87245 "2022-09-14T02:30:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![m\_shah2](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@m\_shah2](https://discourse.julialang.org/u/m_shah2)\
**Post date:** [September 14, 2022, 2:30am UTC](https://discourse.julialang.org/t/bounds-error-while-iterating-over-for-loop/87245/1 "2022-09-14T02:30:30Z")

</div>

I am trying to run a for loop by iterating over aij where aij is of type Vector{CartesianIndex{2}} (alias for Array{CartesianIndex{2}, 1}).

The code is:

for (x, y) in zip(aij)  
xout = find(U[x, :])  
yout = find(U[y, :])  
comm = intersect(xout, yout)  
nc = size(comm, 1)  
for i = 1:nc  
for j = (i+1):nc  
w = comm[i]  
v = comm[j]  
if NA[w, v] == 1  
W[x, y] += 1  
W[x, w] += 1  
W[x, v] += 1  
W[y, w] += 1  
W[y, v] += 1  
W[w, v] += 1  
end  
end  
end  
end

I get an error like:

BoundsError: attempt to access Tuple{CartesianIndex{2}} at index [2]

Stacktrace:  
[1] indexed\_iterate(t::Tuple{CartesianIndex{2}}, i::Int64, state::Int64)  
@ Base .\tuple.jl:88  
[2] top-level scope  
@ .\In[12]:1  
[3] eval  
@ .\boot.jl:368 [inlined]  
[4] include\_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)  
@ Base .\loading.jl:1428

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [September 14, 2022, 3:33am UTC](https://discourse.julialang.org/t/bounds-error-while-iterating-over-for-loop/87245/2 "2022-09-14T03:33:25Z")

</div>

> [@m\_shah2](#):
>
> Tuple{CartesianIndex{2}}

```julia
julia> a = CartesianIndex(3,4)
CartesianIndex(3, 4)

julia> a[2]
4

julia> b = (a,)
(CartesianIndex(3, 4),)

julia> b[2]
ERROR: BoundsError: attempt to access Tuple{CartesianIndex{2}} at index [2]
Stacktrace:
 [1] getindex(t::Tuple, i::Int64)

julia> typeof(a)
CartesianIndex{2}

```

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [September 14, 2022, 5:39am UTC](https://discourse.julialang.org/t/bounds-error-while-iterating-over-for-loop/87245/3 "2022-09-14T05:39:36Z")

</div>

Welcome! Please add a triple backtick fence around your code to make it legible ([Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757)).

Bounds error means you are indexing outside the size of a list.

---

<div class="post-metadata">

**Author:** ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)\
**Post date:** [September 14, 2022, 11:37am UTC](https://discourse.julialang.org/t/bounds-error-while-iterating-over-for-loop/87245/4 "2022-09-14T11:37:43Z")

</div>

> [@m\_shah2](#):
>
> for (x, y) in zip(aij)

You probably want

```julia
for (x, y) in Tuple.(aij)

```

here.

`zip` is for attaching elements of different collections together. That would be necessary in languages where the indices have to be kept in separate arrays by themselves.

Since Julia has `CartesianIndex` type to store the indices together in a single object, `aij` doesn’t need to be `zip`ped. What you need is to instead turn it into a `Tuple` so it can be separated out into `x` and `y` here. The above is one way to do it, but the following:

```julia
for a in aij
  x, y = Tuple(a)

```

maybe a bit more effiient in case `aij` contains a lot of values.

(Using either of these instead of `for (x, y) in zip(aij)` would get rid of the bounds error issue as well.)
