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,711
  • 2
  • 30
  • 48

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
  • 24,909
  • 20
  • 73
  • 94
1

Use set:

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

Test:

>>> L1 = [1,2,5]
>>> L2 = [8,1]
>>> len(set(L1) - set(L2))
2
Kenly
  • 24,317
  • 7
  • 44
  • 60