Search code examples
pythonpython-3.xlistset-difference

Get uncommon items between 2 lists


I have 2 lists 'a1' and 'a2':

a1 = [[1, 4], [1, 10], [2, 5], [2, 11], [3, 6], [4, 7], [4, 12], [5, 8], [5, 13], 
      [6, 9], [7, 14], [8, 15], [2, 10], [3, 11], [5, 12], [6, 13], [8, 14], [9, 15]]

a2 = [[1, 10], [2, 11], [4, 12], [5, 13], [7, 14], [8, 15], [2, 10], [3, 11], [5, 12], 
      [6, 13], [8, 14], [9, 15]]

I am looking to create a third list that contains the uncommon elements between the 2 previous lists, I should have something like this.

[[1, 4], [2, 5], [3, 6], [4, 7], [5, 8], [6, 9]]

Set difference could be applied to extract the elements of the first list that do not contain the second.

a3 = a1.difference(a2)

The problem is that it works for 2 lists, not for 2 lists of lists as in this case.

Is there an effective solution for this type of case? Best regards.


Solution

  • [x for x in a1 if x not in a2]