title: filter list of list by matching with lists in other list regardless of their position and group the result
body: <p>I have 2 lists of lists and my code can filter list1 based on matching with elements in list2:
  Means return lists inside list1 which shares the same last element with any list inside list2</p>
  <pre><code>list1 = [[1,2,3], [4,5,16], [9, 0, 50]]
  list2 = [[9,8,50], [7,10,3]]
  
  list2_ids = {x[-1] for x in list2}
  result = [x for x in list1 if x[-1] in list2_ids]
  
  #result
  [1,2,3]
  [9, 0, 50]
  
  </code></pre>
  <p>I want to group the filtered lists from list1 to to groups based on their first element
  if their share the same last element with another list in list2 AND share the first element too.</p>
  <p>from my example:</p>
  <pre><code>lists_with_shared_first_and_last_element = [9, 0, 50]
  lists_with_shared_last_element_and_different_first = [1,2,3]
  </code></pre>