# How to unfold a set of tuples?

**URL:** <https://discourse.julialang.org/t/how-to-unfold-a-set-of-tuples/95696>\
**Category:** General Usage\
**Tags:** question, tuple\
**Created:** [March 7, 2023, 9:17pm UTC](https://discourse.julialang.org/t/how-to-unfold-a-set-of-tuples/95696 "2023-03-07T21:17:42Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Optimization](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/optimization/32/32462_2.png) [@Optimization](https://discourse.julialang.org/u/Optimization)\
**Post date:** [March 7, 2023, 9:17pm UTC](https://discourse.julialang.org/t/how-to-unfold-a-set-of-tuples/95696/1 "2023-03-07T21:17:42Z")

</div>

I have a set of tuples something like A = [(1,2),(,7,10),(3,2),(9,8),(5,1)] and wanted to unwrap them into a set. So, for “A” output would be like B = {1,2,3,5,7,8,9,10}

Is there any way to do this (without using for loop, ideally)?

---

<div class="post-metadata">

**Author:** ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)\
**Post date:** [March 7, 2023, 9:20pm UTC](https://discourse.julialang.org/t/how-to-unfold-a-set-of-tuples/95696/2 "2023-03-07T21:20:45Z")

</div>

What about

```julia
reduce(union, Set.(A))

```

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [March 7, 2023, 9:31pm UTC](https://discourse.julialang.org/t/how-to-unfold-a-set-of-tuples/95696/3 "2023-03-07T21:31:06Z")

</div>

`Set(Iterators.flatten(A))` gets you there.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [March 7, 2023, 10:41pm UTC](https://discourse.julialang.org/t/how-to-unfold-a-set-of-tuples/95696/4 "2023-03-07T22:41:00Z")

</div>

Any clues why `unique(Iterators.flatten(A))` is so much slower than `Set`?

---

<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:** [March 7, 2023, 11:01pm UTC](https://discourse.julialang.org/t/how-to-unfold-a-set-of-tuples/95696/5 "2023-03-07T23:01:12Z")

</div>

Well, it allocates an array in addition to (internally) creating a `Set`.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [March 7, 2023, 11:08pm UTC](https://discourse.julialang.org/t/how-to-unfold-a-set-of-tuples/95696/6 "2023-03-07T23:08:40Z")

</div>

```julia
collect(Set(Iterators.flatten(A)))

```

is much faster than:

```julia
unique(Iterators.flatten(A))

```

what is then the point of unique?

---

<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:** [March 7, 2023, 11:36pm UTC](https://discourse.julialang.org/t/how-to-unfold-a-set-of-tuples/95696/7 "2023-03-07T23:36:27Z")

</div>

> [@rafael.guerra](#):
>
> what is then the point of unique?

It preserves the order of the elements. `collect(Set(...))` does not.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [March 7, 2023, 11:52pm UTC](https://discourse.julialang.org/t/how-to-unfold-a-set-of-tuples/95696/8 "2023-03-07T23:52:07Z")

</div>

Thanks Steve. The order is not the first thing that comes to mind from the unique name, but you and the manual never lie.

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [March 8, 2023, 12:32pm UTC](https://discourse.julialang.org/t/how-to-unfold-a-set-of-tuples/95696/9 "2023-03-08T12:32:05Z")

</div>

> [@rafael.guerra](#):
>
> ```julia
> collect(Set(Iterators.flatten(A)))
> 
> ```
> 
> is much faster than:
> 
> ```julia
> unique(Iterators.flatten(A))
> 
> ```

Is it?

```julia
julia> @btime collect(Set(Iterators.flatten($A)))
  204.793 ns (5 allocations: 528 bytes)

julia> @btime unique(Iterators.flatten($A))
  227.554 ns (6 allocations: 544 bytes)

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [March 8, 2023, 12:33pm UTC](https://discourse.julialang.org/t/how-to-unfold-a-set-of-tuples/95696/10 "2023-03-08T12:33:01Z")

</div>

It is:

```julia
A = [(rand(Int8), rand(Int8)) for _ in 1:10_000]

Set(Iterators.flatten(A)) # 11 μs ( 7 allocs: 1.4 KiB)
collect(Set(Iterators.flatten(A))) # 13 μs ( 8 allocs: 1.7 KiB)
unique(Iterators.flatten(A)) # 121 μs (18 allocs: 4.5 KiB)

```
