# UndefVarError with list comprehensions, but not with for...end

**URL:** <https://discourse.julialang.org/t/undefvarerror-with-list-comprehensions-but-not-with-for-end/53237>\
**Category:** General Usage\
**Created:** [January 12, 2021, 5:31pm UTC](https://discourse.julialang.org/t/undefvarerror-with-list-comprehensions-but-not-with-for-end/53237 "2021-01-12T17:31:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![hdavid16](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hdavid16/32/11531_2.png) [@hdavid16](https://discourse.julialang.org/u/hdavid16)\
**Post date:** [January 12, 2021, 5:31pm UTC](https://discourse.julialang.org/t/undefvarerror-with-list-comprehensions-but-not-with-for-end/53237/1 "2021-01-12T17:31:26Z")

</div>

It would be nice to be able to do the following without the `UndefVarError`:

```julia
julia> reduce(+, 1 for i in 1:2, j in i:3; init = 0)
ERROR: UndefVarError: i not defined
Stacktrace:
 [1] top-level scope at none:1

julia> sum(1 for i in 1:2, j in i:3)
ERROR: UndefVarError: i not defined
Stacktrace:
 [1] top-level scope at none:1

```

I understand you can workaround it using `if` statements:

```julia
julia> sum(1 for i in 1:2, j in 1:3 if j>=i)
5

```

but it would be nice to not have to do it this way…

Having iterators depend on other iterators works with standard `for` loops:

```julia
julia> for i in 1:2, j in i:3
       1
       end

```

It would also be great if you could create `sparse` arrays using similar notation:

```julia
julia> [1 for i in 1:2, j in i:3]
ERROR: UndefVarError: i not defined
Stacktrace:
 [1] top-level scope at none:1

```

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [January 12, 2021, 6:04pm UTC](https://discourse.julialang.org/t/undefvarerror-with-list-comprehensions-but-not-with-for-end/53237/2 "2021-01-12T18:04:29Z")

</div>

I believe you need to replace the comma by another `for` and then it should work:

```julia
julia> sum(1 for i in 1:2 for j in i:3)
5

```

---

<div class="post-metadata">

**Author:** ![hdavid16](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hdavid16/32/11531_2.png) [@hdavid16](https://discourse.julialang.org/u/hdavid16)\
**Post date:** [January 12, 2021, 6:08pm UTC](https://discourse.julialang.org/t/undefvarerror-with-list-comprehensions-but-not-with-for-end/53237/3 "2021-01-12T18:08:00Z")

</div>

That’s an even better workaround. Would be nice to not have the extra `for` in there though…  
Thanks @dpsanders

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [January 12, 2021, 6:25pm UTC](https://discourse.julialang.org/t/undefvarerror-with-list-comprehensions-but-not-with-for-end/53237/4 "2021-01-12T18:25:35Z")

</div>

The `,` makes a matrix in the context of a comprehension / generator. But you don’t have a matrix when `j` depends on `i`.

---

<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:** [January 12, 2021, 6:51pm UTC](https://discourse.julialang.org/t/undefvarerror-with-list-comprehensions-but-not-with-for-end/53237/5 "2021-01-12T18:51:35Z")

</div>

they do different things:

```julia
julia> [x+y for x in 1:2, y in 2:3]
2×2 Matrix{Int64}:
 3 4
 4 5

julia> [x+y for x in 1:2 for y in 2:3]
4-element Vector{Int64}:
 3
 4
 4
 5

```

---

<div class="post-metadata">

**Author:** ![hdavid16](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hdavid16/32/11531_2.png) [@hdavid16](https://discourse.julialang.org/u/hdavid16)\
**Post date:** [January 12, 2021, 7:23pm UTC](https://discourse.julialang.org/t/undefvarerror-with-list-comprehensions-but-not-with-for-end/53237/6 "2021-01-12T19:23:13Z")

</div>

That makes sense. It would be nice if it made a sparse matrix when needed so that you could use it in a more straightforward fashion in `sum` or `reduce` as is done when creating variable arrays in [JuMP](https://jump.dev/JuMP.jl/stable/variables/#variable_sparseaxisarrays-1):

```julia
julia> @variable(model, x[i=1:2, j=i:2])
JuMP.Containers.SparseAxisArray{VariableRef,2,Tuple{Int64,Int64}} with 3 entries:
  [1, 2] = x[1,2]
  [2, 2] = x[2,2]
  [1, 1] = x[1,1]

```

But then, I guess you could just have a custom `sum` within JuMP to do sums over these variables…
