# Delete special part from a list using Mathematica

I have a list like this:

{{-0.0019,1},{-0.0018,2},...,{0,11},{0.001,6},...}


I need to make a new list that start from zero value. like this:

{{0,11},{0.001,6},...}


How can I do?

• try Cases[{a_,_}/;>=0]@yourlist or Select[Positive@*First]@yourlist
– kglr
Commented May 22, 2023 at 7:36
• @kglr I receive this error: Syntax::: "{a_,_}/;" cannot be followed by ">=0". Commented May 22, 2023 at 7:42
• @kglr Thank's. this Select[Positive@*First]@yourlist is true. Commented May 22, 2023 at 7:44
• sorry, it should be Cases[{a_,_}/;a>=0]@yourlist
– kglr
Commented May 22, 2023 at 7:51

There are probably 10 ways to do this.

lst = {{-0.0019, 1}, {-0.0018, 2}, {0, 11}, {0.001, 6}, {0.002, 6}}
lst[[  First@FirstPosition[lst, {0, _}] ;; -1]]


You can also do

p = FirstPosition[lst, {0, _}]
lst[[Range[First@p, Length@lst]]]


Update

Actually Cases is not what you want. Since you want to start your new list only after the {0,_} entry in the old list.

a new list that start from zero value

For example. given

lst = {{1, 10}, {-0.0019, 1}, {-0.0018, 2}, {0, 11}, {0.001,6}, {0.002, 6}}
Cases[lst, {x_, _} /; x >= 0]


Gives

But you really want

Same issue with Select[Positive@*First]@yourlist.

So I think using FirstPosition is what you want to use.

And if I want new list with just before zero?

lst = {{1, 10}, {-0.0019, 1}, {-0.0018, 2}, {0, 11}, {0.001, 6}, {0.002, 6}}
p = First@FirstPosition[lst, {0, _}]
If[p > 1, lst[[1;;p-1]], {}]


lst = {{0,20},{1, 10}, {-0.0019, 1}, {-0.0018, 2}, {0, 11}, {0.001, 6}, {0.002, 6}}
p = First@FirstPosition[lst, {0, _}]
If[p > 1, lst[[1;;p-1]], {}]


ps. This assumes at least one {0,_} entry exists in the list. Otherwise, I would add a check for NotFound being returned from FirstPosition as well to make code more robust.

• Thank you so much. Commented May 22, 2023 at 7:46
• Thanks.I don't care this point. And if I want new list with just before zero? Commented May 22, 2023 at 8:05
• @caren I would still use FirstPosition but now count from 1 up to one less that that value. Updated. Just need special case, for when {0,_} shows on first position. Commented May 22, 2023 at 8:14
• Be negative or positive value it's not important. I want to separate list from this position. Commented May 23, 2023 at 10:36

### Drop + LengthWhile

Drop[#, LengthWhile[#, First @ # != 0 &]] & @ lst

{{0, 11}, {0.001, 6}, {0.002, 6}}


### NestWhile + Rest

NestWhile[Rest, lst, #[[1, 1]] != 0 &]

{{0, 11}, {0.001, 6}, {0.002, 6}}


### ReplaceAll

ReplaceAll[{___, a : {0, _}, b___} :> {a, b}]@lst

{{0, 11}, {0.001, 6}, {0.002, 6}}


Another way, using FirstCase and Position:

lst = {{1, 10}, {-0.0019, 1}, {-0.0018, 2}, {0, 11}, {0.001, 6}, {0.002, 6}};

lst[[Det@Position[lst, FirstCase[lst, {0, _}]] ;;]]

(*{{0, 11}, {0.001, 6}, {0.002, 6}}*)


Or using FirstPosition and TakeList:

Last@TakeList[lst, {FirstPosition[lst, {0, _}][[1]] - 1, UpTo[Length[lst]]}]

(* (*{{0, 11}, {0.001, 6}, {0.002, 6}}*)*)


Thanks for your excellent observation, Nasser!

• This does not really work. Same issue as with Cases., Try it on this lst = {{1, 10}, {-0.0019, 1}, {-0.0018, 2}, {0, 11}, {0.001, 6}, {0.002, 6}}; it should return {{0, 11}, {0.001, 6}, {0.002, 6}} but your solution returns {{1, 10}, {0, 11}, {0.001, 6}, {0.002, 6}} . Commented May 22, 2023 at 17:59
• @Nasser Thanks, mate! Commented May 22, 2023 at 19:35
• nice solution. Now it works. Commented May 22, 2023 at 19:43
• I'm so grateful. Commented May 23, 2023 at 6:46

Let n be the index of the original list of pairs pairList such that newList = pairList[[n;;]] is desired and for each element elem in newList, First@elem is non-negative. Then the desired list is yielded by

pairList = {{1, 10}, {-0.0019, 1}, {-0.0018, 2}, {0, 11}, {0.001, 6}, {0.002, 6}};
SplitBy[pairList, NonNegative@*First] // Last


{{0, 11}, {0.001, 6}, {0.002, 6}}

• I want to separate the list from this position. Commented May 23, 2023 at 10:43
list = {{-0.0019, 1}, {-0.0018, 2}, {0, 11}, {0.001, 6}, {0.002, 6}};


Using GroupBy

Values @ KeySelect[# >= 0 &] @ GroupBy[list, First, First]


{{0, 11}, {0.001, 6}, {0.002, 6}}