# Np.select equivalent to deal with multiple conditions

**URL:** <https://discourse.julialang.org/t/np-select-equivalent-to-deal-with-multiple-conditions/87639>\
**Category:** New to Julia\
**Tags:** question, array\
**Created:** [September 22, 2022, 12:23pm UTC](https://discourse.julialang.org/t/np-select-equivalent-to-deal-with-multiple-conditions/87639 "2022-09-22T12:23:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![CompulsoryCoffee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/compulsorycoffee/32/30434_2.png) [@CompulsoryCoffee](https://discourse.julialang.org/u/CompulsoryCoffee)\
**Post date:** [September 22, 2022, 12:23pm UTC](https://discourse.julialang.org/t/np-select-equivalent-to-deal-with-multiple-conditions/87639/1 "2022-09-22T12:23:25Z")

</div>

Hi,

I am looking for an equivalent to np.select.

How would you reproduce the following python code in Julia so it would work with n conditions?

```julia
x = np.arange(6)
condlist = [x<3, x>3]
choicelist = [x, x**2]
np.select(condlist, choicelist, 42)
array([0, 1, 2, 42, 16, 25])

```

Thanks!

---

<div class="post-metadata">

**Author:** ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)\
**Post date:** [September 22, 2022, 1:45pm UTC](https://discourse.julialang.org/t/np-select-equivalent-to-deal-with-multiple-conditions/87639/2 "2022-09-22T13:45:40Z")

</div>

I’m not aware of an equivalent to that function, but it doesn’t seem too hard to implement. (And since this is Julia, our implementations can be as fast as ones from libraries.)

```julia
julia> x = 0:5
0:5

julia> condmat = [x .< 3 x .> 3]
6×2 BitMatrix:
 1 0
 1 0
 1 0
 0 0
 0 1
 0 1

julia> choicemat = [x x.^2]
6×2 Matrix{Int64}:
 0 0
 1 1
 2 4
 3 9
 4 16
 5 25

julia> function conditionalselect(conditionmatrix, choicematrix, default = zero(eltype(choicematrix)))
         @assert size(conditionmatrix) == size(choicematrix)
         result = fill(default, size(choicematrix, 1))
         for (rownum, conditionrow) in pairs(eachrow(conditionmatrix))
           choice = findfirst(conditionrow)
           if !isnothing(choice)
             result[rownum] = choicematrix[rownum, choice]
           end
         end
         result
       end
conditionalselect (generic function with 1 method)

julia> conditionalselect(condmat, choicemat, 42)
6-element Vector{Int64}:
  0
  1
  2
 42
 16
 25

```

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [September 22, 2022, 2:34pm UTC](https://discourse.julialang.org/t/np-select-equivalent-to-deal-with-multiple-conditions/87639/3 "2022-09-22T14:34:24Z")

</div>

```julia
julia> [x<3 ? x : x>3 ? x^2 : 42 for x in 0:5]
7-element Vector{Int64}:
  0
  1
  2
 42
 16
 25

```

is simple and readily extendible.

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [September 22, 2022, 2:38pm UTC](https://discourse.julialang.org/t/np-select-equivalent-to-deal-with-multiple-conditions/87639/4 "2022-09-22T14:38:16Z")

</div>

Since this is Julia discourse: it also only allocates the final Vector, not all those intermediates.

---

<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:** [September 22, 2022, 2:48pm UTC](https://discourse.julialang.org/t/np-select-equivalent-to-deal-with-multiple-conditions/87639/5 "2022-09-22T14:48:59Z")

</div>

Although it’s possible to implement a `select` function in Julia, I would re-think your whole approach. For both performance and clarity you really want to do this sort of thing in a _single_ loop or comprehension as demonstrated by @Dan [above](https://discourse.julialang.org/t/np-select-equivalent-to-deal-with-multiple-conditions/87639/3).

The Matlab or Numpy style of doing a sequence of “vectorized” calls [leaves a lot of performance on the table](https://julialang.org/blog/2017/01/moredots/#isnt_vectorized_code_already_fast) because it constructs a sequence of temporary arrays just to discard them at the end. It’s also, in my opinion, a lot clearer to write a single loop in many cases.

Loops are fast in Julia; you don’t need to fear them like you would in Python. Programming doesn’t have to be an exercise in “mining” the standard library and hoping you can find just the function that you need.

---

<div class="post-metadata">

**Author:** ![CompulsoryCoffee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/compulsorycoffee/32/30434_2.png) [@CompulsoryCoffee](https://discourse.julialang.org/u/CompulsoryCoffee)\
**Post date:** [September 22, 2022, 2:54pm UTC](https://discourse.julialang.org/t/np-select-equivalent-to-deal-with-multiple-conditions/87639/6 "2022-09-22T14:54:39Z")

</div>

Thanks a lot for the help

I start to really love this language! (few more GIS capabilities and I am sold 🙂 )

---

<div class="post-metadata">

**Author:** ![CompulsoryCoffee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/compulsorycoffee/32/30434_2.png) [@CompulsoryCoffee](https://discourse.julialang.org/u/CompulsoryCoffee)\
**Post date:** [September 22, 2022, 3:05pm UTC](https://discourse.julialang.org/t/np-select-equivalent-to-deal-with-multiple-conditions/87639/7 "2022-09-22T15:05:35Z")

</div>

thanks for the tip - definitely useful to keep in mind when coming from Python
