# Way to efficiently view lists of lists as a read-only flattened list?

**URL:** <https://discourse.julialang.org/t/way-to-efficiently-view-lists-of-lists-as-a-read-only-flattened-list/14721>\
**Category:** General Usage\
**Tags:** question\
**Created:** [September 9, 2018, 12:17am UTC](https://discourse.julialang.org/t/way-to-efficiently-view-lists-of-lists-as-a-read-only-flattened-list/14721 "2018-09-09T00:17:27Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![djsegal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/djsegal/32/13752_2.png) [@djsegal](https://discourse.julialang.org/u/djsegal)\
**Post date:** [September 9, 2018, 12:17am UTC](https://discourse.julialang.org/t/way-to-efficiently-view-lists-of-lists-as-a-read-only-flattened-list/14721/1 "2018-09-09T00:17:27Z")

</div>

Let’s say you have a list of lists that you want to view as a single list:

```julia
super_list = [
    [1 , 2 , ... , 1001234],
    [6 , 1 , ... , 3],
    ...
    [42]
]

# _squash is some function that does read-only vcat'ing
squashed_list = _squash(super_list)

@assert in(42, squashed_list)

```

What is an efficient way to make a view over these lists, with indexing as if they were all `vcat`’ed together?

// this treats the `squashed_list` as a read-only array

---

<div class="post-metadata">

**Author:** ![gpapo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gpapo/32/1857_2.png) [@gpapo](https://discourse.julialang.org/u/gpapo)\
**Post date:** [September 9, 2018, 12:56am UTC](https://discourse.julialang.org/t/way-to-efficiently-view-lists-of-lists-as-a-read-only-flattened-list/14721/2 "2018-09-09T00:56:39Z")

</div>

`Iterators.flatten(list)` should be your solution, it does not allocate.

If you need indexing you could use `IterTools.nth` function over the `flatten`ed vector.

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [September 9, 2018, 1:47am UTC](https://discourse.julialang.org/t/way-to-efficiently-view-lists-of-lists-as-a-read-only-flattened-list/14721/3 "2018-09-09T01:47:01Z")

</div>

It looks like Iterators.jl is deprecated, and that functionality isn’t replaced. So I dont think this resolves the issue.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [September 9, 2018, 5:24am UTC](https://discourse.julialang.org/t/way-to-efficiently-view-lists-of-lists-as-a-read-only-flattened-list/14721/4 "2018-09-09T05:24:59Z")

</div>

It got deprecated because a lot of the functionality moved into Base. See [here](https://docs.julialang.org/en/stable/base/iterators/#Base.Iterators.flatten) for the `flatten` function.

The old packages README doesn’t really make that clear, maybe a PR indicating the change would be good.

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [September 9, 2018, 5:31am UTC](https://discourse.julialang.org/t/way-to-efficiently-view-lists-of-lists-as-a-read-only-flattened-list/14721/5 "2018-09-09T05:31:15Z")

</div>

Thanks. I was confused because neither `flatten` nor `Iterators` are exported for me. I have to use `using Base.Iterators` to get it. That’s expected, right?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [September 9, 2018, 9:11am UTC](https://discourse.julialang.org/t/way-to-efficiently-view-lists-of-lists-as-a-read-only-flattened-list/14721/6 "2018-09-09T09:11:11Z")

</div>

> [@pdeffebach](#):
>
> I have to use `using Base.Iterators` to get it.

No, the module `Iterators` is in the namespace, so just use `Iterators.flatten` directly.

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [September 9, 2018, 1:55pm UTC](https://discourse.julialang.org/t/way-to-efficiently-view-lists-of-lists-as-a-read-only-flattened-list/14721/7 "2018-09-09T13:55:03Z")

</div>

thanks. I’ll look into what’s going on, then.

---

<div class="post-metadata">

**Author:** ![hessammehr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hessammehr/32/3847_2.png) [@hessammehr](https://discourse.julialang.org/u/hessammehr)\
**Post date:** [October 20, 2018, 11:30pm UTC](https://discourse.julialang.org/t/way-to-efficiently-view-lists-of-lists-as-a-read-only-flattened-list/14721/8 "2018-10-20T23:30:22Z")

</div>

Like @djsegal, I’ve been struggling to find an efficient idiomatic way to `reduce` a function over the individual elements of an `Array{Array{T}}`. Even when the reducing function doesn’t need to allocate, say something like `max`, I still see quite a bit of allocations.

```julia
x = [rand(100) for i in 1:50]
# assuming f itself doesn't allocate - e.g. max
f1(x) = reduce(max, Iterators.flatten(x))
@benchmark f1($x)
# memory estimate: 312.52 KiB
# allocs estimate: 10001

f2(x) = reduce(max, (z for y=x for z=y))
@benchmark f2($x)
# memory estimate: 314.88 KiB
# allocs estimate: 10102

```

I would appreciate it if anyone could suggest a way to achieve this without allocations. It would be a shame to have to write a for loop.
