Take the 2-minute tour ×

Question : How do you filter based on other items in a list?

I got a List that looks like that


I want to keep the tupples which got the biggest second numbers when they got the same first ones

so something like that on output


with (2,1) removed because the 1 was < then 2 in (2,2)

so i need to filter based on other objects in the List how do you do that.

Efficiency is not really important since the list got at maximum 171 items

share|improve this question

2 Answers 2

up vote 10 down vote accepted
for ((x, y) <- lst if !lst.exists(t => x == t._1 && y < t._2)) yield (x, y)

But if you want non-quadratic complexity:

share|improve this answer
thanks, didn't know about groupby –  pastjean Dec 19 '10 at 21:56
are you quite sure about that second example? groupBy will return a Map[Int, (Int, Int)] –  Kevin Wright Dec 19 '10 at 22:43
Someday I'll stop being surprised people would rather take a wild guess than paste a line into the repl. –  extempore Dec 20 '10 at 6:24
extempore is right folk! The same goes for actually testing an answer before you accept it as well... –  Kevin Wright Dec 20 '10 at 7:57
Show no mercy to thy maps. :D –  axel22 Dec 20 '10 at 17:48

Converting a list of pairs to a map will use the last occurring entry when a given "key" occurs twice.

And a Tuple is sorted on the first element, then the second element, etc.


// = List((2,1),(2,2),(3,1)).toMap
// = Map((2,2), (3,1))

Just convert back to a list with .toList afterwards, if necessary

share|improve this answer

Your Answer


