-3

I have two lists of lists:

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

arr2 = [[2,3,6,1],
        [8,1,3],
        [5,5,6]]

I need to check which elements from arr2 aren't contained in arr1 and delete those elements from arr2.

So result must be:

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

6 and 8 aren't contained in arr1, so it deleted in arr2.

How to do that?

Alex Savin
  • 215
  • 1
  • 4
  • 12
  • Try searching for "list intersection" - e,g, http://stackoverflow.com/questions/642763/python-intersection-of-two-lists – DNA Jan 06 '17 at 13:08
  • Even `7` isn't in `arr1`, How is it present in `arr2`? Kindly [edit] your post and add a [mcve]. See [ask]. – Bhargav Rao Jan 06 '17 at 13:10

1 Answers1

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

arr2 = [[2, 3, 6, 1],
        [8, 1, 3],
        [7, 5, 6]]

set1 = set(sum(arr1, []))
print('Elements found in arr1:')
print(set1)

arr3 = [[x for x in sub if x in set1]
        for sub in arr2]

print('Sublists of arr3:')
for sub in arr3:
    print(sub)

Output:

Elements found in arr1:
set([1, 2, 3, 5])
Sublists of arr3:
[2, 3, 1]
[1, 3]
[5]
Alex Hall
  • 34,833
  • 5
  • 57
  • 89