# When Should One Use \`(...)\` or \`\[...\]\` for Array Comprehensions

**URL:** <https://discourse.julialang.org/t/when-should-one-use-or-for-array-comprehensions/60423>\
**Category:** General Usage\
**Tags:** arrays\
**Created:** [May 2, 2021, 3:40pm UTC](https://discourse.julialang.org/t/when-should-one-use-or-for-array-comprehensions/60423 "2021-05-02T15:40:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![RoyiAvital](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/royiavital/32/571_2.png) [@RoyiAvital](https://discourse.julialang.org/u/RoyiAvital)\
**Post date:** [May 2, 2021, 3:40pm UTC](https://discourse.julialang.org/t/when-should-one-use-or-for-array-comprehensions/60423/1 "2021-05-02T15:40:26Z")

</div>

When Should One Use `(...)` or `[...]` for Array Comprehensions?  
What is the difference between the 2?

From Slack:

```julia
julia> @btime [x^2 for x in 1:10000];
  6.519 μs (2 allocations: 78.20 KiB)julia> @btime (x^2 for x in 1:10000);
  0.015 ns (0 allocations: 0 bytes)julia> @btime sum([x^2 for x in 1:10000]);
  8.855 μs (2 allocations: 78.20 KiB)julia> @btime sum((x^2 for x in 1:10000));
  1.409 ns (0 allocations: 0 bytes)julia> @btime sum(x^2 for x in 1:10000);
  1.410 ns (0 allocations: 0 bytes)

```

The code above was by @kimlaberinto.

Some answers were:

> `(x^2 for x in 1:10000)` is just producing a generator.

> The difference is that `()` is lazy while `[]` is eager. If you perform a reduction (like `sum(x^2 for x in 1:10000)` ), you won’t allocate anything.

Someone also mentioned one is heap allocated while the other is stack allocated.

Please add more information on the subject.

P. S.  
I’m putting it here for knowledge preservation of a discussion I read (Not participated) on Slack.

---

<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:** [May 2, 2021, 4:28pm UTC](https://discourse.julialang.org/t/when-should-one-use-or-for-array-comprehensions/60423/2 "2021-05-02T16:28:12Z")

</div>

Some methods explicitly expect `AbstractArray` as a parameter, not just any iterable in this case they will not work with a generator, a most basic example in this class is `getindex`.

Also if you materialize the array you can mutate it later, which might be relevant in some contexts.

---

<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:** [May 2, 2021, 4:42pm UTC](https://discourse.julialang.org/t/when-should-one-use-or-for-array-comprehensions/60423/3 "2021-05-02T16:42:40Z")

</div>

> [@RoyiAvital](#):
>
> When Should One Use `(...)` or `[...]` for Array Comprehensions?

One thing to mention is that only `[...]` is an array comprehension, because it creates an array. Generators, `(...)`, don’t, and aren’t called array comprehensions.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [May 3, 2021, 7:11am UTC](https://discourse.julialang.org/t/when-should-one-use-or-for-array-comprehensions/60423/4 "2021-05-03T07:11:11Z")

</div>

This [post](https://discourse.julialang.org/t/are-generator-expressions-documented-also-other-questions/13786/7) provides advanced analysis of the topic.

In the Julia manual, comprehensions written without the enclosing square brackets using [generator expressions](https://docs.julialang.org/en/v1/manual/arrays/#Generator-Expressions) lie in the same [Multi-dimensional Arrays](https://docs.julialang.org/en/v1/manual/arrays/#man-multi-dim-arrays) section as comprehensions written with enclosing square brackets.
