# How to find elements in a list satisfying different criteria

I am not good at Mathematica. I have the following list already ordered according to the second part:

list1 = {{3, 2}, {5, 2}, {7, 2}, {7, 4}, {8, 4}, {12, 8}, {94, 8}, {4, 10}, {42, 10}};


I want to select the first elements where the second part gets bigger. The result would be:

list2 = {{3, 2}, {7, 4}, {12, 8}, {4, 10}}


One approach goes like this:

First /@ GatherBy[list1, Last]


It turns out that GatherBy can be replaced by SplitBy.

• Wow! Thank you very much! – user57467 Dec 18 '18 at 14:16
• @user57467 Glad to be helpful. – Αλέξανδρος Ζεγγ Dec 18 '18 at 14:24

You can also use SequenceReplace:

SequenceReplace[list1, {p : ({_, a_} ..)} :> First[{p}]]


{{3, 2}, {7, 4}, {12, 8}, {4, 10}}

• This function is still not included in my version. I will study it in the future. Thank you very much! – user57467 Dec 19 '18 at 16:10

This seems to work.

DeleteDuplicatesBy[list1, Last]


{{3, 2}, {7, 4}, {12, 8}, {4, 10}}