# Can you define variables inside a comprehension?

**URL:** https://discourse.julialang.org/t/can-you-define-variables-inside-a-comprehension/85642
**Category:** General Usage
**Tags:** scope
**Created:** [August 11, 2022, 10:03pm UTC](https://discourse.julialang.org/t/can-you-define-variables-inside-a-comprehension/85642 "2022-08-11T22:03:26Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Albert\_Zevelev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albert_zevelev/32/11844_2.png) [@Albert\_Zevelev](https://discourse.julialang.org/u/Albert_Zevelev)
#### Post date: [August 11, 2022, 10:03pm UTC](https://discourse.julialang.org/t/can-you-define-variables-inside-a-comprehension/85642/1 "2022-08-11T22:03:26Z")

</div>

```julia
julia> M = [1 2; 3 4]
2×2 Matrix{Int64}:
 1 2
 3 4

julia> M = (M + M') ./ 2.0
2×2 Matrix{Float64}:
 1.0 2.5
 2.5 4.0

julia> M = [1 2; 3 4]
2×2 Matrix{Int64}:
 1 2
 3 4

julia> [x = (x + x') ./ 2.0 for x in [M] ]
1-element Vector{Matrix{Float64}}:
 [1.0 2.5; 2.5 4.0]

julia> M
2×2 Matrix{Int64}:
 1 2
 3 4

```

Is there a way to redefine/mutate a variable inside a comprehension?  
Or are all things supposed to be always be local inside a comprehension?

---

<div class="post-metadata">

### Author: ![thautwarm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thautwarm/32/37760_2.png) [@thautwarm](https://discourse.julialang.org/u/thautwarm)
#### Post date: [August 12, 2022, 12:56am UTC](https://discourse.julialang.org/t/can-you-define-variables-inside-a-comprehension/85642/2 "2022-08-12T00:56:41Z")

</div>

> [@Albert\_Zevelev](#):
>
> Is there a way to redefine/mutate a variable inside a comprehension?

You can change an existing variable from outer scope as follows:

```julia
julia> function f(outer_var)
           [
               begin
                   outer_var = i
               end
               for i in 1:100 ]
           return outer_var
       end
f (generic function with 1 method)

julia> f(1)
100

```

---

<div class="post-metadata">

### Author: ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)
#### Post date: [August 12, 2022, 1:01am UTC](https://discourse.julialang.org/t/can-you-define-variables-inside-a-comprehension/85642/3 "2022-08-12T01:01:53Z")

</div>

> [@Albert\_Zevelev](#):
>
> are all things supposed to be always be local inside a comprehension?

As a matter of style, I’d say it’s better that way.

---

<div class="post-metadata">

### Author: ![dawbarton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dawbarton/32/215461_2.png) [@dawbarton](https://discourse.julialang.org/u/dawbarton)
#### Post date: [August 12, 2022, 10:27am UTC](https://discourse.julialang.org/t/can-you-define-variables-inside-a-comprehension/85642/4 "2022-08-12T10:27:55Z")

</div>

You can mutate a variable inside a comprehension but I wouldn’t recommend it as its very non-standard/confusing. If you really want to do it then

```julia
julia> M = [1.0 2; 3 4]
2×2 Matrix{Float64}:
 1.0 2.0
 3.0 4.0

julia> [x .= (x + x') ./ 2.0 for x in [M]]
1-element Vector{Matrix{Float64}}:
 [1.0 2.5; 2.5 4.0]

julia> M
2×2 Matrix{Float64}:
 1.0 2.5
 2.5 4.0

```

(Note that you need to get the type of `M` right to mutate it like this.)

If you don’t want the resulting collection, a much cleaner way would be to declare a function and broadcast over the array.

```julia
julia> dosomething!(x) = (x .= (x + x') ./ 2.0)
dosomething! (generic function with 1 method)

julia> M = [1.0 2; 3 4]
2×2 Matrix{Float64}:
 1.0 2.0
 3.0 4.0

julia> dosomething!.([M])
1-element Vector{Matrix{Float64}}:
 [1.0 2.5; 2.5 4.0]

julia> M
2×2 Matrix{Float64}:
 1.0 2.5
 2.5 4.0

```

or

```julia
julia> M = [1.0 2; 3 4]
2×2 Matrix{Float64}:
 1.0 2.0
 3.0 4.0

julia> foreach(x -> x .= (x + x') ./ 2.0, [M])

julia> M
2×2 Matrix{Float64}:
 1.0 2.5
 2.5 4.0

```
