# What is this for syntax called?

**URL:** <https://discourse.julialang.org/t/what-is-this-for-syntax-called/87445>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [September 18, 2022, 7:03pm UTC](https://discourse.julialang.org/t/what-is-this-for-syntax-called/87445 "2022-09-18T19:03:26Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![AwesomeQuest](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/awesomequest/32/38910_2.png) [@AwesomeQuest](https://discourse.julialang.org/u/AwesomeQuest)\
**Post date:** [September 18, 2022, 7:03pm UTC](https://discourse.julialang.org/t/what-is-this-for-syntax-called/87445/1 "2022-09-18T19:03:26Z")

</div>

> [i for i in 1:10]

What is this called and how exactly does it work? I get how it’s used (wonderfully convenient), but what is this exactly equivalent to? What and how does it allocate? When should and shouldn’t I use it?

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [September 18, 2022, 7:04pm UTC](https://discourse.julialang.org/t/what-is-this-for-syntax-called/87445/2 "2022-09-18T19:04:55Z")

</div>

It’s called a list comprehension [Single- and multi-dimensional Arrays · The Julia Language](https://docs.julialang.org/en/v1/manual/arrays/#man-comprehensions)

> but what is this exactly equivalent to?

It is a syntax for lazy map, filter, and a products over iterators. That is,

If you write

```julia
[f(x, y) for x in xs, y in ys if g(x, y)]

```

then that is equivalent to

```julia
itr1 = Iterators.product(xs, ys)
itr2 = Iterators.filter(((x, y),) -> g(x, y), itr1)
itr3 = Iterators.map(((x, y),) -> f(x, y), itr2)
collect(itr3)

```

allocating an array, usually without any intermediates.

If you instead wrote

```julia
(f(x, y) for x in xs, y in ys if g(x, y))

```

then that would be called a “generator comprehension” and this would not do the final `collect` stage, just returning an unmaterialized iterator.

---

<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:** [September 18, 2022, 7:15pm UTC](https://discourse.julialang.org/t/what-is-this-for-syntax-called/87445/3 "2022-09-18T19:15:46Z")

</div>

> [@Mason](#):
>
> It’s called a list comprehension

The name i normally hear is ‘array comprehension’, since, unlike python’s lists, Julia comprehensions produce arrays.

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [September 18, 2022, 7:24pm UTC](https://discourse.julialang.org/t/what-is-this-for-syntax-called/87445/4 "2022-09-18T19:24:11Z")

</div>

I just call them list comprehensions (or just ‘comprehensions’), and I think a lot of the community does too, but sure, if you want to be as precise about it as possible, one can call it an array comprehension.

I guess you feel that the word ‘list’ here should always refer to a ‘linked list’, but to be honest I think linked lists are so irrelevant in julia-land that they don’t really deserve to reserve the name ‘list’. As far as I’m concerned, our arrays are lists of items. They’re just not implemented as linked list structures.

---

<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:** [September 18, 2022, 8:14pm UTC](https://discourse.julialang.org/t/what-is-this-for-syntax-called/87445/5 "2022-09-18T20:14:40Z")

</div>

I mainly meant that I’ve almost only ever heard ‘array comprehension’ on this forum, and whenever someone says ‘list comprehension’ they were corrected. So I assumed the former was the official name.

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [September 18, 2022, 8:30pm UTC](https://discourse.julialang.org/t/what-is-this-for-syntax-called/87445/6 "2022-09-18T20:30:02Z")

</div>

Huh. If that’s the case I never noticed. Either way, ‘array’ would be a more accurate name so good to point out.

---

<div class="post-metadata">

**Author:** ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)\
**Post date:** [September 18, 2022, 8:38pm UTC](https://discourse.julialang.org/t/what-is-this-for-syntax-called/87445/7 "2022-09-18T20:38:53Z")

</div>

Out of curiosity, I searched for the usage of either of those phrases over the last year on this forum… and it turns out they were used the exact same number of times:  
[28 uses of “array comprehension”](https://discourse.julialang.org/search?q=%22array%20comprehension%22%20after%3A2021-09-19) vs [28 uses of “list comprehension”](https://discourse.julialang.org/search?q=%22list%20comprehension%22%20after%3A2021-09-19)

---

<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:** [September 18, 2022, 8:46pm UTC](https://discourse.julialang.org/t/what-is-this-for-syntax-called/87445/8 "2022-09-18T20:46:57Z")

</div>

I believe ‘array comprehension’ is up by one, actually😉

---

<div class="post-metadata">

**Author:** ![yha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yha/32/3502_2.png) [@yha](https://discourse.julialang.org/u/yha)\
**Post date:** [September 18, 2022, 8:54pm UTC](https://discourse.julialang.org/t/what-is-this-for-syntax-called/87445/9 "2022-09-18T20:54:10Z")

</div>

> [@Mason](#):
>
> I guess you feel that the word ‘list’ here should always refer to a ‘linked list’, but to be honest I think linked lists are so irrelevant in julia-land that they don’t really deserve to reserve the name ‘list’. As far as I’m concerned, our arrays are lists of items. They’re just not implemented as linked list structures.

There are other distinctions between “list” and “array” that are probably more relevant: Julia array comprehensions can produce arrays of any dimensionality, and generally try to mimic the `axes` of the things you iterate over (e.g. offset axes, named axes, block arrays):

```julia
julia> using OffsetArrays

julia> X = -1:1;

julia> Y = OffsetArray(reshape(1:6,3,:), 2, -3);

julia> [(x,y) for x in X, y in Y]
3×3×2 OffsetArray(::Array{Tuple{Int64, Int64}, 3}, 1:3, 3:5, -2:-1) with eltype Tuple{Int64, Int64} with indices 1:3×3:5×-2:-1:
[:, :, -2] =
 (-1, 1) (-1, 2) (-1, 3)
 (0, 1) (0, 2) (0, 3)
 (1, 1) (1, 2) (1, 3)

[:, :, -1] =
 (-1, 4) (-1, 5) (-1, 6)
 (0, 4) (0, 5) (0, 6)
 (1, 4) (1, 5) (1, 6)

```

---

<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:** [September 18, 2022, 10:18pm UTC](https://discourse.julialang.org/t/what-is-this-for-syntax-called/87445/10 "2022-09-18T22:18:53Z")

</div>

> [@Mason](#):
>
> As far as I’m concerned, our arrays are lists of items. They’re just not implemented as linked list structures

(Neither are Python lists, which are simply arrays of object pointers internally, exactly like `Vector{Any}` in Julia.)
