# Best way to return lists of unknown length?

**URL:** <https://discourse.julialang.org/t/best-way-to-return-lists-of-unknown-length/7631>\
**Category:** New to Julia\
**Created:** [December 8, 2017, 6:37pm UTC](https://discourse.julialang.org/t/best-way-to-return-lists-of-unknown-length/7631 "2017-12-08T18:37:45Z")\
**Posts on this page:** 7\
**Page:** 1

<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:** [December 8, 2017, 6:37pm UTC](https://discourse.julialang.org/t/best-way-to-return-lists-of-unknown-length/7631/1 "2017-12-08T18:37:46Z")

</div>

In Python, if I were to make a function which would return a list whose length varies depending on the input, if I couldn’t fit the code into a list comprehension, I’d usually use a yield statement to yield each array element I want in the output. Is there a recommended pattern for doing this in Julia?

One could always create an empty list and then append each wanted entry to that list but I wonder if there’s a better way to do that? Speed is a very crucial factor here as this function will need to be applied to every element in a very large matrix.

* * *

In case anyone needs an example of what I actually mean, I’m trying to convert something similar to this function from Python to Julia:

```julia
def hop(x, n):
    for i in range(n-1):
        mask = 3 << i
        if x & mask != 0 and x & mask != mask:
            yield x ^ mask
    mask = (1 << (n-1)) | 1
    if x & mask != 0 and x & mask != mask:
        yield x ^ mask

```

What’s going on here is that you give this function two integers `x` and `n`, where you care about the binary pattern of x mod n. Ie. if I am concerned with the binary pattern `1010` then `x` should be `10` in base `10` and `n` should be `4` because I’m treating `x` as a `4` bit integer.

The function is making a mask, initially 3, ie. `0011` in base `2` and its checking checking if `x` shares exactly one `1` bit with `mask` and if so, will yield an integer whose binary pattern is the same those of `x` in the spots where `mask` has zeros and will yield the complement of `x`’s digits in the positions where `mask` has `1`s. Finally, it does the same thing with a `mask` that has `1`s at both ends of the digit and `0`s everywhere else.

From the amount of control structure here, I don’t think this is a good candidate for list comprehensions but I also suspect that creating an empty list and appending to it isn’t ideal.

Any ideas?

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [December 8, 2017, 6:51pm UTC](https://discourse.julialang.org/t/best-way-to-return-lists-of-unknown-length/7631/2 "2017-12-08T18:51:50Z")

</div>

A loop that pushes elements onto a vector maybe?

---

<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:** [December 8, 2017, 7:04pm UTC](https://discourse.julialang.org/t/best-way-to-return-lists-of-unknown-length/7631/3 "2017-12-08T19:04:24Z")

</div>

> [@Mason](#):
>
> creating an empty list and appending to it isn’t ideal

I assume you mean a `Vector`. I would say that it ideal, especially if you know the element type. `push!` is very smart, in the sense that it allocates more space when it needs to expand the vector. Also see `sizehint!`.

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [December 8, 2017, 9:25pm UTC](https://discourse.julialang.org/t/best-way-to-return-lists-of-unknown-length/7631/4 "2017-12-08T21:25:28Z")

</div>

Check out [ResumableFunctions.jl](https://github.com/BenLauwens/ResumableFunctions.jl) which is the alternative of Python’s `yield`-style functions in Julia.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [December 8, 2017, 9:28pm UTC](https://discourse.julialang.org/t/best-way-to-return-lists-of-unknown-length/7631/5 "2017-12-08T21:28:51Z")

</div>

There is no need for that here. Just `push!` to a `Vector`.

---

<div class="post-metadata">

**Author:** ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)\
**Post date:** [December 9, 2017, 11:26am UTC](https://discourse.julialang.org/t/best-way-to-return-lists-of-unknown-length/7631/6 "2017-12-09T11:26:24Z")

</div>

> [@mohamed82008](#):
>
> Check out ResumableFunctions.jl which is the alternative of Python’s yield-style functions in Julia.

Yes, unless you actually need a `Vector` for the result, and you are processing the returned elements, then something like that (or writing an iterator type, with `done`, `next`, and `start` methods), can do better, esp. with very large (or unbounded) amounts of data.

---

<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:** [December 9, 2017, 1:46pm UTC](https://discourse.julialang.org/t/best-way-to-return-lists-of-unknown-length/7631/7 "2017-12-09T13:46:52Z")

</div>

> [@Mason](#):
>
> In Python, if I were to make a function which would return a list whose length varies depending on the input, if I couldn’t fit the code into a list comprehension, I’d usually use a yield statement to yield each array element I want in the output. Is there a recommended pattern for doing this in Julia?

The allocation-free analogue of this in Julia is to [write an iterator](https://docs.julialang.org/en/release-0.4/manual/interfaces/?highlight=iterator#iteration) (i.e. create a type with `start`/`next`/`done` methods). This way you don’t need to allocate a list (vector) at all and can just loop over the results.

In some cases, you won’t need to define a custom iterator type, and can instead use a [`Generator` expression](https://docs.julialang.org/en/stable/manual/arrays/#Generator-Expressions-1).
