# Dropping some row from a Table with If condition [closed]

I have to drop the rows of a table only when the element in the first coloumn of the row satisfies multiple condition.

The table:

tab = Table[{i, i^2}, {i, 1, 10}]
{{1, 1}, {2, 4}, {3, 9}, {4, 16}, {5, 25}, {6, 36}, {7, 49}, {8, 64}, {9, 81}, {10, 100}}


What i tried:

dim = Dimensions[tab][]
For[i = dim, i > 0, i--,
If[tab[[i, 1]] > 8||tab[[i, 1]] < 2,
tab = Drop[tab, i, None]]]


What it does:

tab
{}


What i expect

tab
{{2, 4}, {3, 9}, {4, 16}, {5, 25}, {6, 36}, {7, 49}, {8, 64}}

• Try Select[Table[{i, i^2}, {i, 1, 10}], First[#] <= 8 &]. – J. M.'s technical difficulties Jun 5 at 18:36
• Although, using For is generally discouraged; if you are going to use it in this instance, learn the syntax for Drop. Note that tab = Drop[tab, i] drops multiple rows, i.e., the first i rows. You want tab = Drop[tab, {i}] which will just drop row i. – Bob Hanlon Jun 5 at 19:11

DeleteCases[tab, _?(#[] < 2 || #[] > 8 &)]


Select the wanted:

f1 = Function[i, {i, i^2}]
tab = Table[f1[i], {i, 1, 10}]
keep = Function[i, 9 > i > 1] (* the selection predicate *)
Select[tab, keep@*First]


Or, don't create the unwanted:

f2 = Function[i, If[keep[i], {i, i^2}, Nothing]]
Table[f2[i], {i, 10}]


You could also use Except with Cases...

Cases[tab,Except[{a_,b_}/;a>8||a<2]]


{{2,4},{3,9},{4,16},{5,25},{6,36},{7,49},{8,64}}

The OP wants to drop the rows of a table only when the values in the first column satisfy multiple conditions, specifically (as an example) when the values in the first column are less than 2 and greater than 8.

Two powerful functions that may be useful here are Select and Pick

Select

As J. M.'s technical difficulties neatly showed in a comment, and in an astute criticism of my original post, Select may be used in the following manner to solve the problem:

Select[tab, 2 <= First[#] <= 8 &]


{{2, 4}, {3, 9}, {4, 16}, {5, 25}, {6, 36}, {7, 49}, {8, 64}}

Alternatively (again due to J. M.'s technical difficulties), Between may be used to avoid slots completely:

Select[tab, Between[{2, 8}] @* First]


{{2, 4}, {3, 9}, {4, 16}, {5, 25}, {6, 36}, {7, 49}, {8, 64}}

Two other variants with Select (from original post):

Select[tab, First[#]>=2 && First[#] <= 8 &]


and:

Select[tab, !(First[#]<2 || First[#] > 8) &]


Pick

Maybe something like the following:

Pick[tab,Between[{2, 8}]/@tab[[All,1]]]


{{2, 4}, {3, 9}, {4, 16}, {5, 25}, {6, 36}, {7, 49}, {8, 64}}

Pick[tab,#>8 || #<2&/@tab[[All,1]],False]

• (+1 of course.) Whenever possible, I prefer to write selection conditions of this kind in interval format: Select[tab, 2 <= First[#] <= 8 &]. In particular, using Between[] allows for writing a slot-free version: Select[tab, Between[{2, 8}] @* First]. – J. M.'s technical difficulties Jun 6 at 10:14