-2

Is there any way to detect distinction after two lists comparison, if second list is more important?

List1 items: 1 2 3 4
List2 items: 1 2   4

Expected result should be: remove 3

List1 items: 1 2 3 4 
List2 items: 1 2 3 4 5

Expected result should be: no further changes

List1 items: 1 2 3 4 5
List2 items: 1 2 3 4 6

Expected result should be: remove 5

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
DzLL
  • 59
  • 2
  • 7

2 Answers2

1

set(list1) - set(list2) will return a set of missing items.

L3viathan
  • 26,748
  • 2
  • 58
  • 81
0

"I just want to remove items from 1st list, which are not present in 2nd list"

"Expected result should be: remove 3"

In [11]: list1 = {1, 2, 3, 4}
In [12]: list2 = {1, 2, 4}
In [13]: set.difference(list1, list2)
Out[13]: {3}

"Expected result should be: no further changes"

In [14]: list1 = {1, 2, 3, 4}
In [15]: list2 = {1, 2, 3, 4, 5}
In [16]: set.difference(list1, list2)
Out[16]: set()

"Expected result should be: remove 5"

In [17]: list1 = {1, 2, 3, 4, 5}
In [18]: list2 = {1, 2, 3, 4, 6}
In [19]: set.difference(list1, list2)
Out[19]: {5}
antikantian
  • 610
  • 1
  • 4
  • 11