-2

I have 2 list

A=['1','2','3','4','5','6','7','8','t','e','e','e','r','t',','w','w',','r','r']

B=['5','6','7','8','t','e','e','e','r','t',','w','w',','r','r','d','f','w','w','r','r]

I have to extract all elements of A which is not present in B

I am trying

final=[]
for i in A:
   for j in B:
      if i!= j :
        final.append(i)

still i am not getting what i want

petezurich
  • 9,280
  • 9
  • 43
  • 57

2 Answers2

3
final = [i for i in A if i not in B]

Or, if not order nor duplicates are important to preserve:

final = set(A) - set(B)
shx2
  • 61,779
  • 13
  • 130
  • 153
0

You could turn them into type set

A = set(A)
B = set(B)
print(A.difference(B))

>>> {'1', '3', '4', '2'}

more info here