# Divide collection into two groups

**URL:** <https://discourse.julialang.org/t/divide-collection-into-two-groups/127983>\
**Category:** Data\
**Tags:** package\
**Created:** [April 11, 2025, 4:03pm UTC](https://discourse.julialang.org/t/divide-collection-into-two-groups/127983 "2025-04-11T16:03:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![HugoTrentesaux](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hugotrentesaux/32/12583_2.png) [@HugoTrentesaux](https://discourse.julialang.org/u/HugoTrentesaux)\
**Post date:** [April 11, 2025, 4:03pm UTC](https://discourse.julialang.org/t/divide-collection-into-two-groups/127983/1 "2025-04-11T16:03:09Z")

</div>

I can filter a group based on a condition:

```julia
using DataPipes
a = [1,2,5,5,3,9,8]
large = @p filter(_>4, a)
# 5,5,9,8
small = @p filter(_<=4, a)
# 1,2,3

```

But is there a way to achieve the same in a single action?

```julia
# I would like to divide 'a' into two groups like this
large, small = @p divide(_>4, a)

```

If it exists, I do not find the name of the function (I tried `?divide`, `?split`, `?group`, `?separate`…).

---

<div class="post-metadata">

**Author:** ![mtfishman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mtfishman/32/30755_2.png) [@mtfishman](https://discourse.julialang.org/u/mtfishman)\
**Post date:** [April 11, 2025, 4:48pm UTC](https://discourse.julialang.org/t/divide-collection-into-two-groups/127983/2 "2025-04-11T16:48:04Z")

</div>

You could use `group` from SplitApplyCombine.jl:

```julia
julia> using DataPipes, SplitApplyCombine

julia> a = [1, 2, 5, 5, 3, 9, 8]
7-element Vector{Int64}:
 1
 2
 5
 5
 3
 9
 8

julia> d = @p group(_ > 4, a)
2-element Dictionaries.Dictionary{Bool, Vector{Int64}}:
 false │ [1, 2, 3]
  true │ [5, 5, 9, 8]

julia> d[true] # large
4-element Vector{Int64}:
 5
 5
 9
 8

julia> d[false] # small
3-element Vector{Int64}:
 1
 2
 3

```

**EDIT:** Or getting a bit fancier:

```julia
julia> d = @p group(_ > 4 ? "large" : "small", a)
2-element Dictionaries.Dictionary{String, Vector{Int64}}:
 "small" │ [1, 2, 3]
 "large" │ [5, 5, 9, 8]

julia> d["large"]
4-element Vector{Int64}:
 5
 5
 9
 8

julia> d["small"]
3-element Vector{Int64}:
 1
 2
 3

```
