filtering element of one List from another in Scala with different tuple structure I am trying to understand the best way to write this. This is part of my organization work so can't disclose it fully here like why such requirement. So Apologies for that.
I have ListA and ListB.
val ListA: List[((String, String), Int)] = List(
  ((&quot;A&quot;, &quot;B&quot;), 1),
  ((&quot;C&quot;, &quot;D&quot;), 2),
  ((&quot;E&quot;, &quot;F&quot;), 5),
  ((&quot;E&quot;, &quot;F&quot;), 4),
  ((&quot;E&quot;, &quot;F&quot;), 3)
)
val ListB: List[(String, String)] = List(
  (&quot;A&quot;, &quot;B&quot;),
  (&quot;E&quot;, &quot;F&quot;)
)

We need to search id in ListA that contain ListB element. For example tuple (&quot;A&quot;, &quot;B&quot;) is present in ListA with ((&quot;A&quot;, &quot;B&quot;), 1), . so output would be List(1). This process would repeat again once we fetch first match. if no match is found then List(0) would be output. Also both lists are sorted on priority. ListB drives the main priority and after that in case of repeating values the List A does it. And lists are already ordered.
I wrote this.
val result = ((for {
  a &lt;- ListB
  m &lt;- ListA.filter(_._1 == a).map(_._2)
} yield m):::List(0)).slice(0,1)

slice is used because we need List with first match. as ListB is already sorted on priority. Also We are going to repeat this process again by removing already matched id from ListA. But for now please ignore like how we remove these elements and re-iterate.
So new iteration would be on
val ListA: List[((String, String), Int)] = List(
    //  ((&quot;A&quot;, &quot;B&quot;), 1), This will not be part of second iteration as we already matched it in first iteration.
      ((&quot;C&quot;, &quot;D&quot;), 2),
      ((&quot;E&quot;, &quot;F&quot;), 5),
      ((&quot;E&quot;, &quot;F&quot;), 4),
      ((&quot;E&quot;, &quot;F&quot;), 3)
    )
    val ListB: List[(String, String)] = List(
      (&quot;A&quot;, &quot;B&quot;),
      (&quot;E&quot;, &quot;F&quot;)
    )

Output of

iteration 1 is List(1)
iteration 2 is List(5)
iteration 3 is List(4)
iteration 4 is List(3)
iteration 5 is List(0)

I would just like to understand that
&quot;Is it right way to code where we search one list element in another&quot;.
&quot;Do we have some scala library that can be used here like ListA.filter(ListB.contains(_._1)) --&gt; this needs us to write full filter case method so it wont work like this&quot;
Apart from my comment, I'd suggest using Set for, well sets, since operations will likely be quicker, but more importantly, they're more correct wrt. what operations can be done on them.
val setB: Set[(String, String)] = ListB.toSet

val filtered: List[((String, String), Int)] =
  ListA.filterNot(x =&gt; setB.contains(x._1))

val result: List[Int] =
  filtered.map(_._2)

This returns a different result.  As I wrote in my comment I don't quite understand the reasoning behind the algorithm and if it's a real requirement that it's going to be iterative like this, you'll likely have to use the implementation you have right now, i.e. via for.
Also splitting the computation into multiple steps might help with understanding what's going on (especially in a debugger), that's also why I copied over the type signatures as you did.

*If the exact same output is desired*, that'd be this then:
val result = ((for {
  m &lt;- ListA.filter(x =&gt; setB.contains(x._1)).map(_._2)
} yield m):::List(0)).slice(0,1)


Nevermind, the same result requires to walk through the list as in the original post.


Also variables shouldn't start with an upper case, c.f. setB instead of SetB.