Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find difference between two 2D array in python [duplicate]

How do I find the difference between two 2D array in python ?

First array and second array

arr1 = [[1,1],[1,2],[1,3],[1,4],[1,5]]
arr2 = [[1,2],[1,3],[1,4]]

The result I want

result = [[1,1],[1,5]]
like image 916
Jeremy Avatar asked May 19 '26 14:05

Jeremy


1 Answers

You can first convert all element to tuple then use set and difference like below:

>>> set(map(tuple,arr1)).difference(map(tuple,arr2))
{(1, 1), (1, 5)}

>>> list(map(list , set(map(tuple,arr1)).difference(map(tuple,arr2))))
[[1,1],[1,5]]
like image 56
I'mahdi Avatar answered May 21 '26 02:05

I'mahdi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!