# How do I create a list (Array of values) from a Set?

**URL:** <https://discourse.julialang.org/t/how-do-i-create-a-list-array-of-values-from-a-set/18555>\
**Category:** General Usage\
**Created:** [December 11, 2018, 10:57am UTC](https://discourse.julialang.org/t/how-do-i-create-a-list-array-of-values-from-a-set/18555 "2018-12-11T10:57:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![johann.spies](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johann.spies/32/8805_2.png) [@johann.spies](https://discourse.julialang.org/u/johann.spies)\
**Post date:** [December 11, 2018, 10:57am UTC](https://discourse.julialang.org/t/how-do-i-create-a-list-array-of-values-from-a-set/18555/1 "2018-12-11T10:57:46Z")

</div>

How do I get

```julia
collect(ap)
5-element Array{Partition{Int64},1}:
 Set(Set{Int64}[Set([3]), Set([2]), Set([1])])
 Set(Set{Int64}[Set([3]), Set([2, 1])])       
 Set(Set{Int64}[Set([3, 1]), Set([2])])       
 Set(Set{Int64}[Set([2, 3, 1])])              
 Set(Set{Int64}[Set([2, 3]), Set([1])])   

```

into an array like this:

```julia
[
[[3],[2],[1]],
[[3],[2,1]],
[[3,1],[2]],
[[2,3,1]],
[[2,3],[1]]
]

```

In Python I can do:

```julia
In [1]: from ps1_partition import get_partitions

In [2]: ap = get_partitions([1,2,3])

In [3]: l = []

In [4]: for i in ap:
   ...: print(i)
   ...: l.append(i)
   ...:     
[[1, 2, 3]]
[[2, 3], [1]]
[[1, 3], [2]]
[[3], [1, 2]]
[[3], [2], [1]]

In [5]: l
Out[5]: [[[1, 2, 3]], [[2, 3], [1]], [[1, 3], [2]], [[3], [1, 2]], [[3], [2], [1]]]

```

---

<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 11, 2018, 11:22am UTC](https://discourse.julialang.org/t/how-do-i-create-a-list-array-of-values-from-a-set/18555/2 "2018-12-11T11:22:47Z")

</div>

```julia
julia> toarray(s::Union{Set, Vector}) = [toarray.(s)...]
toarray (generic function with 1 method)

julia> toarray(v::Number) = v
toarray (generic function with 2 methods)

julia> ab = [Set(Set{Int64}[Set([3]), Set([2]), Set([1])]),
                Set(Set{Int64}[Set([3]), Set([2, 1])]),
                Set(Set{Int64}[Set([3, 1]), Set([2])]),
                Set(Set{Int64}[Set([2, 3, 1])]),
                Set(Set{Int64}[Set([2, 3]), Set([1])])]
5-element Array{Set{Set{Int64}},1}:
 Set([Set([3]), Set([2]), Set([1])])
 Set([Set([3]), Set([2, 1])])
 Set([Set([3, 1]), Set([2])])
 Set([Set([2, 3, 1])])
 Set([Set([2, 3]), Set([1])])

julia> toarray(ab)
5-element Array{Array{Array{Int64,1},1},1}:
 [[3], [2], [1]]
 [[3], [2, 1]]
 [[3, 1], [2]]
 [[2, 3, 1]]
 [[2, 3], [1]]

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [December 11, 2018, 12:03pm UTC](https://discourse.julialang.org/t/how-do-i-create-a-list-array-of-values-from-a-set/18555/3 "2018-12-11T12:03:02Z")

</div>

This also works:

```julia
(x->collect.(x)).(ab)

```

---

<div class="post-metadata">

**Author:** ![johann.spies](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johann.spies/32/8805_2.png) [@johann.spies](https://discourse.julialang.org/u/johann.spies)\
**Post date:** [December 11, 2018, 12:12pm UTC](https://discourse.julialang.org/t/how-do-i-create-a-list-array-of-values-from-a-set/18555/4 "2018-12-11T12:12:40Z")

</div>

Thanks.

```julia
(x->collect.(x)).(ab)

```

works even where ab has the type

```julia
Array{Partition{Int64},1}

```

where the

```julia
toarray

```

solution does not when the type involves {Partition}.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [December 11, 2018, 12:14pm UTC](https://discourse.julialang.org/t/how-do-i-create-a-list-array-of-values-from-a-set/18555/5 "2018-12-11T12:14:30Z")

</div>

Ah, good.

I also checked the run-time, and it’s quite a bit faster, perhaps because splatting can be expensive.
