# delete pairs $(x,y)$ of a list with $x \le x_{0}$ [duplicate]

How we can remove all pairs {x,y} of a list which begin from a special x.

For example:

list={{1,2},{4,5},{5,9},{8,13},{9,0.1},{15,-0.2},{17,-9},{23,2}};


We want keep pairs in which x=<9:

extractlist={{1,2},{4,5},{5,9},{8,13},{9,0.1}};

• If the x's are ordered (as coolwater points out), then TakeWhile[list, #[[1]] <= 9 &] may be (yet another) option worth considering. Dec 26, 2017 at 0:08

## 5 Answers

list = {{1, 2}, {4, 5}, {5, 9}, {8, 13}, {9, 0.1}, {15, -0.2}, {17, -9}, {23,
2}};

trimmedList = Select[list, #[[1]] <= 9 &]

(* {{1, 2}, {4, 5}, {5, 9}, {8, 13}, {9, 0.1}} *)


Some other ways

trimmedList ==
Cases[list, _?(#[[1]] <= 9 &)] ==
Cases[list, {_?(# <= 9 &), _}] ==
DeleteCases[list, _?(#[[1]] > 9 &)] ==
DeleteCases[list, {_?(# > 9 &), _}]

(* True *)


I think:

extractlist=DeleteCases[list, {x_, y_} /; x > 9]

does the job.

Select[list, #[[1]] <= 9 &]


Select is a reasonably simple method.

If the xs are ordered in list

list[[;; Last[Ordering[Ordering[Append[list[[-1 ;; 1 ;; -1, 1]], 9.]]]] - 1]]

{{1, 2}, {4, 5}, {5, 9}, {8, 13}, {9, 0.1}}


Otherwise

Pick[list, Clip[list[[All, 1]], {9., 9.}, {9., 0}], 9.]

{{1, 2}, {4, 5}, {5, 9}, {8, 13}, {9, 0.1}}

• The one involving Ordering is especially appealing. +1 Dec 23, 2017 at 21:48
list = {{1, 2}, {4, 5}, {5, 9}, {8, 13}, {9, 0.1},
{15, -0.2}, {17, -9}, {23, 2}} /. {x_, _} /; x > 9 -> Sequence[]