# List comprehension can't find an already defined variable

**URL:** <https://discourse.julialang.org/t/list-comprehension-cant-find-an-already-defined-variable/138020>\
**Category:** General Usage\
**Tags:** array, comprehension\
**Created:** [July 9, 2026, 4:39am UTC](https://discourse.julialang.org/t/list-comprehension-cant-find-an-already-defined-variable/138020 "2026-07-09T04:39:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![martin\_sanchez](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/martin_sanchez/32/207525_2.png) [@martin\_sanchez](https://discourse.julialang.org/u/martin_sanchez)\
**Post date:** [July 9, 2026, 4:39am UTC](https://discourse.julialang.org/t/list-comprehension-cant-find-an-already-defined-variable/138020/1 "2026-07-09T04:39:02Z")

</div>

I was dealing with an error and in the end it seems to reduce to array comprehension:

```julia-auto
c=0
z = [1,2,3,4]
println([c+=i for i in z])

```

it throws an error:

> ERROR: LoadError: UndefVarError: `c` not defined in local scope  
> Suggestion: check for an assignment to a local variable that shadows a global of the same name.  
> Stacktrace:

Or:

> ERROR: UndefVarError: `c` not defined  
> Stacktrace:  
> [1] (::var"#1#2")(i::Int64)  
> @ Main ./none:0  
> [2] iterate  
> @ ./generator.jl:47 [inlined]  
> [3] collect(itr::Base.Generator{Vector{Int64}, var"#1#2"})  
> @ Base ./array.jl:834  
> [4] top-level scope  
> @ REPL[4]:1= 0

But something like

```julia-auto
for i in z
        x+=i
       end

```

or

```julia-auto
if 1==1
           x+=1
       end

```

Works.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [July 9, 2026, 7:12am UTC](https://discourse.julialang.org/t/list-comprehension-cant-find-an-already-defined-variable/138020/2 "2026-07-09T07:12:34Z")

</div>

When `c+=i` occurs, there is no pre-existing local `c` variable to access, and `c=0` made a global variable. Comprehensions make a _hard_ local scope, so it will not reassign (or access) a global variable unless you put an explicit `global` statement.

`if` works because it doesn’t make a scope. `for` works because it’s a _soft_ local scope, so it would implicitly reassign a global variable in the REPL or notebooks. It would instead behave like hard local scopes in plainer source files or Julia expressions, though there’s an extra warning:

```julia-auto
julia> begin # REPL context
       c=0
       z = [1,2,3,4]
       for i in z
         println(c+=i)
       end
       end
1
3
6
10

julia> eval(quote # evaluate Julia expression
       c=0
       z = [1,2,3,4]
       for i in z
         println(c+=i)
       end
       end)
┌ Warning: Assignment to `c` in soft scope is ambiguous because a global variable by the same name exists: `c` will be treated as a new local. Disambiguate by using `local c` to suppress this warning or `global c` to assign to the existing global variable.
└ @ REPL[9]:5
ERROR: UndefVarError: `c` not defined in local scope
Suggestion: check for an assignment to a local variable that shadows a global of the same name.

```
