# Is there a way to have a nested expression loop

**URL:** <https://discourse.julialang.org/t/is-there-a-way-to-have-a-nested-expression-loop/86796>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [September 5, 2022, 10:52am UTC](https://discourse.julialang.org/t/is-there-a-way-to-have-a-nested-expression-loop/86796 "2022-09-05T10:52:30Z")\
**Posts on this page:** 5\
**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 5, 2022, 10:52am UTC](https://discourse.julialang.org/t/is-there-a-way-to-have-a-nested-expression-loop/86796/1 "2022-09-05T10:52:30Z")

</div>

I’m trying to unwrap a vector of vectors of vectors into one vector and my first thought is to

```julia
[k for i in vecs, j in i, k in j]
or 
[k for k in j, j in i, i in vecs]

```

```julia
ERROR: UndefVarError: i/j not defined

```

Do I need to use a full for loop or am I missing something?

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [September 5, 2022, 10:57am UTC](https://discourse.julialang.org/t/is-there-a-way-to-have-a-nested-expression-loop/86796/2 "2022-09-05T10:57:59Z")

</div>

Perhaps you mean something like:

```julia
[k for i in vecs for j in 1:i for k in 1:j]

```

Your `i` in `j in i` is an element and not iterable. Same for `j` in `k in j`.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [September 5, 2022, 10:59am UTC](https://discourse.julialang.org/t/is-there-a-way-to-have-a-nested-expression-loop/86796/3 "2022-09-05T10:59:08Z")

</div>

ok, I see vector of vector… ok, it should work, but with `for` and not `,` separated.

```julia
julia> a= [[1,2,3], [2,3,4,5,6] ]
2-element Vector{Vector{Int64}}:
 [1, 2, 3]
 [2, 3, 4, 5, 6]

julia> [j for i in a for j in i]
8-element Vector{Int64}:
 1
 2
 3
 2
 3
 4
 5
 6

```

~~Iterators.flatten(a) isn’t going deep, so it’s a bit tricky with that …~~

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [September 5, 2022, 11:07am UTC](https://discourse.julialang.org/t/is-there-a-way-to-have-a-nested-expression-loop/86796/4 "2022-09-05T11:07:15Z")

</div>

Best to check here: [https://discourse.julialang.org/t/how-do-you-unfold-a-nested-julia-array](https://discourse.julialang.org/t/how-do-you-unfold-a-nested-julia-array)

---

<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 5, 2022, 11:15am UTC](https://discourse.julialang.org/t/is-there-a-way-to-have-a-nested-expression-loop/86796/5 "2022-09-05T11:15:48Z")

</div>

yes! there it is! thank you. easy enough to iterate with flatten.
