# Differences between \`for i = 1:2, j = 1:3\` and \`for i = 1:2 for j = 1:3\` in comprehensions

**URL:** <https://discourse.julialang.org/t/differences-between-for-i-1-2-j-1-3-and-for-i-1-2-for-j-1-3-in-comprehensions/13744>\
**Category:** General Usage\
**Tags:** question\
**Created:** [August 20, 2018, 2:09am UTC](https://discourse.julialang.org/t/differences-between-for-i-1-2-j-1-3-and-for-i-1-2-for-j-1-3-in-comprehensions/13744 "2018-08-20T02:09:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![xinhangliu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xinhangliu/32/5055_2.png) [@xinhangliu](https://discourse.julialang.org/u/xinhangliu)\
**Post date:** [August 20, 2018, 2:09am UTC](https://discourse.julialang.org/t/differences-between-for-i-1-2-j-1-3-and-for-i-1-2-for-j-1-3-in-comprehensions/13744/1 "2018-08-20T02:09:53Z")

</div>

Hi guys, I am confused about these two expressions, why are their results different?

```julia
julia> [(i, j) for i = 1:2 for j = 1:3]
6-element Array{Tuple{Int64,Int64},1}:
 (1, 1)
 (1, 2)
 (1, 3)
 (2, 1)
 (2, 2)
 (2, 3)

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

```

---

<div class="post-metadata">

**Author:** ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)\
**Post date:** [August 20, 2018, 2:20am UTC](https://discourse.julialang.org/t/differences-between-for-i-1-2-j-1-3-and-for-i-1-2-for-j-1-3-in-comprehensions/13744/2 "2018-08-20T02:20:19Z")

</div>

I think the differing behavior is generally useful.

In any case, it seems to be documented:  
[https://docs.julialang.org/en/stable/manual/arrays/#Comprehensions-1](https://docs.julialang.org/en/stable/manual/arrays/#Comprehensions-1)

```julia
A = [F(x,y,...) for x=rx, y=ry, ...]

...

The result is an N-d dense array with dimensions that are the concatenation of the dimensions of the variable ranges rx, ry, etc.

```

.  
And a bit further down:

```julia
Ranges in generators and comprehensions can depend on previous ranges by writing multiple for keywords:

julia> [(i,j) for i=1:3 for j=1:i]
6-element Array{Tuple{Int64,Int64},1}:
 (1, 1)
 (2, 1)
 (2, 2)
 (3, 1)
 (3, 2)
 (3, 3)

In such cases, the result is always 1-d.

```

.  
Note that the latter form allows conditional inclusion of elements in the array, using `if` keyword.

```julia
Generated values can be filtered using the if keyword:

julia> [(i,j) for i=1:3 for j=1:i if i+j == 4]
2-element Array{Tuple{Int64,Int64},1}:
 (2, 2)
 (3, 1)

```

In this case I think it makes sense to return an unshaped list.
