# Is there a name for an iterator that is like \`product(a, a)\` except only distinct-indexed elements are paired

**URL:** <https://discourse.julialang.org/t/is-there-a-name-for-an-iterator-that-is-like-product-a-a-except-only-distinct-indexed-elements-are-paired/64838>\
**Category:** General Usage\
**Created:** [July 17, 2021, 11:54pm UTC](https://discourse.julialang.org/t/is-there-a-name-for-an-iterator-that-is-like-product-a-a-except-only-distinct-indexed-elements-are-paired/64838 "2021-07-17T23:54:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [July 17, 2021, 11:54pm UTC](https://discourse.julialang.org/t/is-there-a-name-for-an-iterator-that-is-like-product-a-a-except-only-distinct-indexed-elements-are-paired/64838/1 "2021-07-17T23:54:54Z")

</div>

I often want to iterator over a iterable in such a way that distinct-index elements are only paired once. This is useful for things like one round round-robin leagues

```julia
for i in 1:length(a)-1
 for j in i+1:length(a)
    ai = a[i]
    aj = a[j]
    # do something with ai and aj
  end
end

```

But I want something like

```julia
for (ai, aj) in StairsIterator(a)
  # do something with ai aj
end

```

---

<div class="post-metadata">

**Author:** ![Alexander\_Knudson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alexander_knudson/32/215656_2.png) [@Alexander\_Knudson](https://discourse.julialang.org/u/Alexander_Knudson)\
**Post date:** [July 18, 2021, 5:54am UTC](https://discourse.julialang.org/t/is-there-a-name-for-an-iterator-that-is-like-product-a-a-except-only-distinct-indexed-elements-are-paired/64838/2 "2021-07-18T05:54:07Z")

</div>

I think `IterTools.subsets` will do what you’re looking for. Something like:

```julia
for (i,j) in IterTools.subsets(1:length(a), Val{2}())
    # do something
end

```

This essentially returns the indices of an upper triangular matrix.

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [July 18, 2021, 6:29am UTC](https://discourse.julialang.org/t/is-there-a-name-for-an-iterator-that-is-like-product-a-a-except-only-distinct-indexed-elements-are-paired/64838/3 "2021-07-18T06:29:53Z")

</div>

Using Subsets is very elegant. I am glad I asked.

---

<div class="post-metadata">

**Author:** ![eliassno](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eliassno/32/18917_2.png) [@eliassno](https://discourse.julialang.org/u/eliassno)\
**Post date:** [July 19, 2021, 12:54pm UTC](https://discourse.julialang.org/t/is-there-a-name-for-an-iterator-that-is-like-product-a-a-except-only-distinct-indexed-elements-are-paired/64838/4 "2021-07-19T12:54:21Z")

</div>

Based on @Alexander_Knudson’s solution for the indices, implementing your `StairsIterator` should be straightforward:

```julia
using IterTools
using Base.Cartesian

StairIterator(a) = (
    (@nextract 2 a d -> a[ids[d]]; @ntuple 2 a)
       for ids in IterTools.subsets(1:length(a), Val{2}())
)

a = rand(3)
println(a)

for (ai, aj) in StairIterator(a)
    println("ai = $ai\t aj = $aj")
end

```
