0

I have two lists and I want to print the difference between them (if there is 1 difference, it should print "1". How can I fix that?

So what I have is:

a= ["1","2","3"]
b= ["1","4","5"]

The answer should be 2.

gtlambert
  • 11,253
  • 2
  • 28
  • 47

2 Answers2

3

It depends on what do you mean by difference. If they are equal in length and you want to find out the difference, do:

c = [i for i in a if i not in b]
print len(c)
Shang Wang
  • 23,393
  • 19
  • 68
  • 91
1

Use set:

print len(set(L1) - set(L2)) 

Test:

>>> L1 = [1,2,5]
>>> L2 = [8,1]
>>> len(set(L1) - set(L2))
2
Kenly
  • 19,646
  • 6
  • 41
  • 57