# Behavior of comprehensions

**URL:** <https://discourse.julialang.org/t/behavior-of-comprehensions/4750>\
**Category:** General Usage\
**Tags:** question\
**Created:** [July 9, 2017, 5:22pm UTC](https://discourse.julialang.org/t/behavior-of-comprehensions/4750 "2017-07-09T17:22:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [July 9, 2017, 5:22pm UTC](https://discourse.julialang.org/t/behavior-of-comprehensions/4750/1 "2017-07-09T17:22:41Z")

</div>

I have just realized that nested comprehensions have two non-obvious properties.  
Consider the following code:

```julia
julia> f(i, j) = (println((i,j)); (i,j))                          
f (generic function with 1 method)                                
                                                                  
julia> [f(i, j) for i = 1:2, j = 3:4]                             
(1, 3)                                                            
(2, 3)                                                            
(1, 4)                                                            
(2, 4)                                                            
2×2 Array{Tuple{Int64,Int64},2}:                                  
 (1, 3) (1, 4)                                                   
 (2, 3) (2, 4)                                                   
                                                                  
julia> [f(i, j) for i = 1:2 for j = 3:4]                          
(1, 3)                                                            
(1, 4)                                                            
(2, 3)                                                            
(2, 4)                                                            
4-element Array{Tuple{Int64,Int64},1}:                            
 (1, 3)                                                           
 (1, 4)                                                           
 (2, 3)                                                           
 (2, 4)                                                           
                                                                  
julia> [f(i, j) for i = 1:2, j = 3:4 if true]                    
(1, 3)                                                            
(2, 3)                                                            
(1, 4)                                                            
(2, 4)                                                            
4-element Array{Tuple{Int64,Int64},1}:                            
 (1, 3)                                                           
 (2, 3)                                                           
 (1, 4)                                                           
 (2, 4)                                                           

```

What we can see:

1. The order of traversal of comprehension with _double for loop_ is different than when using _comma loop_ (as Julia uses column major order);
2. It is allowed to add condition after _comma loop_ and then we get a vector (not a matrix, as documented) although it is natural to expect to get a vector.

I would document them both, but maybe it is clear from the current documentation and I am missing something.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [July 9, 2017, 5:58pm UTC](https://discourse.julialang.org/t/behavior-of-comprehensions/4750/2 "2017-07-09T17:58:35Z")

</div>

[https://docs.julialang.org/en/latest/manual/arrays/#Comprehensions-1](https://docs.julialang.org/en/latest/manual/arrays/#Comprehensions-1)
