# How to add several values for one index in a comprehension?

**URL:** <https://discourse.julialang.org/t/how-to-add-several-values-for-one-index-in-a-comprehension/106208>\
**Category:** New to Julia\
**Created:** [November 14, 2023, 1:11pm UTC](https://discourse.julialang.org/t/how-to-add-several-values-for-one-index-in-a-comprehension/106208 "2023-11-14T13:11:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![homocomputeris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/homocomputeris/32/8933_2.png) [@homocomputeris](https://discourse.julialang.org/u/homocomputeris)\
**Post date:** [November 14, 2023, 1:11pm UTC](https://discourse.julialang.org/t/how-to-add-several-values-for-one-index-in-a-comprehension/106208/1 "2023-11-14T13:11:23Z")

</div>

What would be the idiomatic way to get array like `[1, -1, 2, -2, 3, -3]` (or in general `[f(idx1), g(idx1), f(idx2), g(idx2)]` which have several values per one index following one another).

`[(i,-i) for i in 1:5]` gives a list of tuples, `[(i,-i)... for i in 1:5]` is not allowed, and `[i,-i for i in 1:5]` doesn’t work.

---

<div class="post-metadata">

**Author:** ![frylock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frylock/32/50213_2.png) [@frylock](https://discourse.julialang.org/u/frylock)\
**Post date:** [November 14, 2023, 1:14pm UTC](https://discourse.julialang.org/t/how-to-add-several-values-for-one-index-in-a-comprehension/106208/2 "2023-11-14T13:14:58Z")

</div>

EDIT: now I see what you were trying to do with `...` … so … never mind. 🙂

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [November 14, 2023, 1:15pm UTC](https://discourse.julialang.org/t/how-to-add-several-values-for-one-index-in-a-comprehension/106208/3 "2023-11-14T13:15:07Z")

</div>

I’m not sure if I would call this idiomatic, but this works:

```julia
julia> collect(Iterators.flatten((i, -i) for i in 1:5 ))
10-element Vector{Int64}:
  1
 -1
  2
 -2
  3
 -3
  4
 -4
  5
 -5

```

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [November 14, 2023, 2:13pm UTC](https://discourse.julialang.org/t/how-to-add-several-values-for-one-index-in-a-comprehension/106208/4 "2023-11-14T14:13:31Z")

</div>

If you really require two iterables `f` and `g`, zipping and flattening them works: `collect(Iterators.flatten(zip(1:5, -1:-1:-5)))`.

If you can base it off one iterable `1:5`, one-liners generally still involve `flatten`. You can actually pull this off with plain [comprehension implementing nested loops with `flatten`](https://docs.julialang.org/en/v1/base/iterators/#Base.Iterators.flatten): `[sign*absval for absval in 1:5 for sign in (1, -1)]`.

> [@lmiq](#):
>
> `collect(Iterators.flatten((i, -i) for i in 1:5 ))`

v1.9 added `Iterators.flatmap` to write this a bit shorter (not sure why else): `collect(Iterators.flatmap(i -> (i, -i), 1:5))`. One of the documented examples is almost like this example.

tbh I would probably fill an uninitialized array in plain loops directly describing the sequence before I figure out the various one-liners for brevity and verify their type-inferrability.
