# How to use Map inside Select

Input:

Select[Range, 2 <= # <= 5 || 8 <= # <= 10 &]


Output:

{2, 3, 4, 5, 8, 9, 10}


In case I have lots of conditions I want to have them defined separately like this:

Input:

cond = {{2, 5}, {8, 10}};
Or @@ (#[] <= q <= #[] & /@ cond)


Output:

2 <= q <= 5 || 8 <= q <= 10


How to combine my first input with my second input to have all the code in my formula?

• Just use Between[]: Select[Range, Between[{{2, 5}, {11, 19}, {26, 33}, {44, 49}}]] Jan 25, 2020 at 1:02
• OK. But I would like to know also how to do it with Map. In case I wanted a more complex conditions not just simple inequalities. Jan 25, 2020 at 1:06
• Would something like With[{sel = ReleaseHold[Hold[Function][Apply[Or, # <= Hold[Slot] <= #2 & @@@ {{2, 5}, {11, 19}, {26, 33}, {44, 49}}]]]}, Select[Range, sel]] suit your needs? Jan 25, 2020 at 1:17
• Define a helper function cond = {{2, 5}, {8, 10}}; test[x_] = Or @@ (#[] <= x <= #[] & /@ cond); Select[Range, test] Jan 25, 2020 at 2:02

You could use Cases with a Condition:

Cases[Range, q_ /; 2<=q<=5||8<=q<=10]


{2, 3, 4, 5, 8, 9, 10}

or Select with a pure Function:

Select[Range, Function[q, 2<=q<=5||8<=q<=10]]


{2, 3, 4, 5, 8, 9, 10}