# Is it possible to update Array elements in a list comprehension?

**URL:** <https://discourse.julialang.org/t/is-it-possible-to-update-array-elements-in-a-list-comprehension/47855>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [October 6, 2020, 1:38pm UTC](https://discourse.julialang.org/t/is-it-possible-to-update-array-elements-in-a-list-comprehension/47855 "2020-10-06T13:38:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![aachener](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aachener/32/18112_2.png) [@aachener](https://discourse.julialang.org/u/aachener)\
**Post date:** [October 6, 2020, 1:38pm UTC](https://discourse.julialang.org/t/is-it-possible-to-update-array-elements-in-a-list-comprehension/47855/1 "2020-10-06T13:38:44Z")

</div>

I found list comprehension is very useful tool.

Currently I encountered a problem. Say there is an array of 5 integers named TEST. I want to add 1 to randomly selected elements of TEST for 10 times and see if there is a zero in the result.

I can do it in a normal loop, but my question is if I can do it using comprehension?

```julia
julia> TEST = zeros(Int,5)
5-element Array{Int64,1}:
 0
 0
 0
 0
 0

#if I do the following, a error message jumps.
julia> TEST[rand(1:5)] += 1 for i in 1:10
ERROR: syntax: extra token "for" after end of expression
Stacktrace:
 [1] top-level scope at REPL[72]:1
 [2] include_string(::Function, ::Module, ::String, ::String) 
at .\loading.jl:1088

# and if I enclose the expression with (), it returns a generator.
julia> (TEST[rand(1:5)] += 1 for i in 1:10)
Base.Generator{UnitRange{Int64},var"#43#44"}(var"#43#44"(), 1:10)

```

Please help me out and tell me how to update array elements using comprehension.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [October 6, 2020, 1:41pm UTC](https://discourse.julialang.org/t/is-it-possible-to-update-array-elements-in-a-list-comprehension/47855/2 "2020-10-06T13:41:34Z")

</div>

> [@aachener](#):
>
> `TEST[rand(1:5)] += 1 for i in 1:10`

Try enclosing the expression in square brackets.

```julia
[TEST[rand(1:5)] += 1 for i in 1:10]

```

In Julia, they’re called ‘array comprehensions’.

---

<div class="post-metadata">

**Author:** ![aachener](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aachener/32/18112_2.png) [@aachener](https://discourse.julialang.org/u/aachener)\
**Post date:** [October 6, 2020, 1:48pm UTC](https://discourse.julialang.org/t/is-it-possible-to-update-array-elements-in-a-list-comprehension/47855/3 "2020-10-06T13:48:38Z")

</div>

Thanks a lot for your quick response. I think it solved my problem! 😃

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [October 6, 2020, 1:51pm UTC](https://discourse.julialang.org/t/is-it-possible-to-update-array-elements-in-a-list-comprehension/47855/4 "2020-10-06T13:51:57Z")

</div>

> [@DNF](#):
>
> Try enclosing the expression in square brackets.

Note that using an array comprehension for this allocates an unnecessary array; it would be simpler and faster to use a loop:

```julia
for i = 1:10
    TEST[rand(1:5)] += 1
end

```

or, if you care about performance, [put it in a function](https://docs.julialang.org/en/v1/manual/performance-tips/#Avoid-global-variables):

```julia
function randinc!(a, trials)
    r = eachindex(a)
    for i = 1:trials
        a[rand(r)] += 1
    end
    return a
end

```

and then call `randinc!(TEST, 10)`.

There’s nothing wrong with loops in Julia. Loops are fast and often convenient!

> [@aachener](#):
>
> I can do it in a normal loop

(Just noticed that you already realized this. I’m not sure why you want to use a comprehension for this?)

You may also be thinking of a [generator expression](https://docs.julialang.org/en/v1/manual/arrays/#Generator-Expressions), which you can create by using parentheses:

```julia
julia> (TEST[rand(1:5)] += 1 for i in 1:10)
Base.Generator{UnitRange{Int64},var"#11#12"}(var"#11#12"(), 1:10)

```

A generator expression doesn’t actually perform the loop, however; it merely returns an iterator that can be passed an argument to _another_ function that performs the loop, such as `foreach` or `sum`:

```julia
julia> sum(TEST[rand(1:5)] += 1 for i in 1:10)
17

julia> foreach(identity, TEST[rand(1:5)] += 1 for i in 1:10)

```

(When it is passed as an argument, a generator expression doesn’t need the extra parentheses.)

It’s not clear what the point of using a generator, rather than a simple loop, would be in the contest, however.

---

<div class="post-metadata">

**Author:** ![aachener](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aachener/32/18112_2.png) [@aachener](https://discourse.julialang.org/u/aachener)\
**Post date:** [October 6, 2020, 1:56pm UTC](https://discourse.julialang.org/t/is-it-possible-to-update-array-elements-in-a-list-comprehension/47855/5 "2020-10-06T13:56:48Z")

</div>

Thank you for your advice, really helpful!
