-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
  • 358
  • 2
  • 13

2 Answers2

1

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

hchw
  • 1,190
  • 6
  • 13
0

Why not use sets:

set(A) - set(B)

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

Jussi Nurminen
  • 2,025
  • 1
  • 7
  • 15