# What am I doing wrong in this array comprehension?

**URL:** https://discourse.julialang.org/t/what-am-i-doing-wrong-in-this-array-comprehension/62016
**Category:** General Usage
**Tags:** question, array
**Created:** [May 28, 2021, 8:36pm UTC](https://discourse.julialang.org/t/what-am-i-doing-wrong-in-this-array-comprehension/62016 "2021-05-28T20:36:40Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![stephenjfox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stephenjfox/32/12837_2.png) [@stephenjfox](https://discourse.julialang.org/u/stephenjfox)
#### Post date: [May 28, 2021, 8:36pm UTC](https://discourse.julialang.org/t/what-am-i-doing-wrong-in-this-array-comprehension/62016/1 "2021-05-28T20:36:40Z")

</div>

In the midst of writing some ring arithmetic code, I found my Julia session producing very strange results for an element-wise modulo operation.

```julia
v = collect(1:20)
[(i, n) for i=1:20, n=(v .^2 .% 17) if i == n] # produces 19 tuples of (n, n)

"""
    test_that_above_is_incorrect()

Show that the tuple isn't incrementing the index, or is throwing out the
value assigned to `i`. Whatever it is doing, applying the same logic in
a loop shows the only found element is unity.
"""
function test_that_above_is_incorrect()
    out = []
    for (counter, n) in enumerate(1:20)
        if (n ^2) % 17 == counter
            push!(out, n)
        end
    end
    out # outputs a vector of a single element
end

```

I know the logical equivalent with `enumerate` work correctly i.e.

```julia
[(i,n) for (i,n) in enumerate(v) if i == ((n ^ 2) % 17)]

```

is just fine.

So where did I go astray in the initial comprehension?

Thanks in advance

---

<div class="post-metadata">

### Author: ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)
#### Post date: [May 28, 2021, 9:44pm UTC](https://discourse.julialang.org/t/what-am-i-doing-wrong-in-this-array-comprehension/62016/2 "2021-05-28T21:44:42Z")

</div>

The comprehension generates the outer product of the two iterators.

```julia
julia> [(i,j) for i=1:3, j=1:3]
3×3 Matrix{Tuple{Int64, Int64}}:
 (1, 1) (1, 2) (1, 3)
 (2, 1) (2, 2) (2, 3)
 (3, 1) (3, 2) (3, 3)

```

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [May 28, 2021, 9:57pm UTC](https://discourse.julialang.org/t/what-am-i-doing-wrong-in-this-array-comprehension/62016/3 "2021-05-28T21:57:24Z")

</div>

For equivalence with enumerate you should zip (or use `pairs`):  
`[(i, n) for (i,n) in zip(1:20,v) if i==(n^2)%17]`  
what you have is a double loop instead.

---

<div class="post-metadata">

### Author: ![stephenjfox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stephenjfox/32/12837_2.png) [@stephenjfox](https://discourse.julialang.org/u/stephenjfox)
#### Post date: [May 28, 2021, 9:58pm UTC](https://discourse.julialang.org/t/what-am-i-doing-wrong-in-this-array-comprehension/62016/4 "2021-05-28T21:58:34Z")

</div>

That’s interesting. In additional testing, it seems the `if` just selects a column of that outer product. Thanks for clarifying

---

<div class="post-metadata">

### Author: ![stephenjfox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stephenjfox/32/12837_2.png) [@stephenjfox](https://discourse.julialang.org/u/stephenjfox)
#### Post date: [May 28, 2021, 10:01pm UTC](https://discourse.julialang.org/t/what-am-i-doing-wrong-in-this-array-comprehension/62016/5 "2021-05-28T22:01:41Z")

</div>

I was under the impression the double loop was a bit more explicit, a la

```julia
[(i, n) for i in 1:20 for n in (v .^ 2 % 17) if i==n]

```

---

<div class="post-metadata">

### Author: ![cjdoris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cjdoris/32/213133_2.png) [@cjdoris](https://discourse.julialang.org/u/cjdoris)
#### Post date: [May 30, 2021, 9:06pm UTC](https://discourse.julialang.org/t/what-am-i-doing-wrong-in-this-array-comprehension/62016/6 "2021-05-30T21:06:37Z")

</div>

You get a product iterator either way, but using commas will get you a matrix (or higher dim tensor with more components).
