-1

I have two lists

A=[1,3,5,6] 
B=[3,5,7]

I need to get only the elements that are part of A but not part of B. Thus, the result of this must be Result= [1,6]

I'm trying to get the difference, but the output I get is the following

Result = [i for i in A + B if i not in A or i not in B]
Result = [1,6,7]

How could I get [1,6] instead?

TheObands
  • 378
  • 1
  • 3
  • 16

2 Answers2

2

result = [i for i in A if i not in B]

hchw
  • 1,388
  • 8
  • 14
0

Why not use sets:

set(A) - set(B)

Sets comprise unique elements, and subtraction corresponds to set difference.

Jussi Nurminen
  • 2,257
  • 1
  • 9
  • 16