# How to accomplish \`\[x\*y for x in 1:y, y in 1:5\]\`?

**URL:** <https://discourse.julialang.org/t/how-to-accomplish-x-y-for-x-in-1-y-y-in-1-5/63522>\
**Category:** New to Julia\
**Created:** [June 25, 2021, 1:50am UTC](https://discourse.julialang.org/t/how-to-accomplish-x-y-for-x-in-1-y-y-in-1-5/63522 "2021-06-25T01:50:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![maxkapur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maxkapur/32/21208_2.png) [@maxkapur](https://discourse.julialang.org/u/maxkapur)\
**Post date:** [June 25, 2021, 1:50am UTC](https://discourse.julialang.org/t/how-to-accomplish-x-y-for-x-in-1-y-y-in-1-5/63522/1 "2021-06-25T01:50:46Z")

</div>

How can I make a nested list comprehension where the inner iterator depends on the outer one?

Using a for loop, I can do

```julia
res = []
for y in 1:5, x in 1:y
    push!(res, x*y)
end

```

but I would like to accomplish the same using an expression like `[x*y for x in 1:y, y in 1:5]`, if possible.

Edit: I just figured out

```julia
vcat([[x*y for x in 1:y] for y in 1:5]...)

```

although my understanding is that splatting is to be avoided if possible.

---

<div class="post-metadata">

**Author:** ![jlapeyre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlapeyre/32/4514_2.png) [@jlapeyre](https://discourse.julialang.org/u/jlapeyre)\
**Post date:** [June 25, 2021, 2:12am UTC](https://discourse.julialang.org/t/how-to-accomplish-x-y-for-x-in-1-y-y-in-1-5/63522/2 "2021-06-25T02:12:14Z")

</div>

> [@maxkapur](#):
>
> `vcat([[x*y for x in 1:y] for y in 1:5]...)`

This gives the same result

`[x*y for y in 1:5 for x in 1:y]`

---

<div class="post-metadata">

**Author:** ![maxkapur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maxkapur/32/21208_2.png) [@maxkapur](https://discourse.julialang.org/u/maxkapur)\
**Post date:** [June 25, 2021, 2:36am UTC](https://discourse.julialang.org/t/how-to-accomplish-x-y-for-x-in-1-y-y-in-1-5/63522/3 "2021-06-25T02:36:50Z")

</div>

Thank you. I was stuck because I didn’t expect to have to move the inner iterator to the outside.

I guess the justification for this design is that this puts the `x` and `y` iterators in the same order as the equivalent for loop. Is this compatibility principle consistent across languages? I checked and Python works like Julia in this regard:

```nohighlight
In [2]: [x*y for y in range(5) for x in range(y)]
Out[2]: [0, 0, 2, 0, 3, 6, 0, 4, 8, 12]

```
