Python code to return total count of no. of positions in which items are differing at same index
<p>A=[1,2,3,4,5,6,7,8,9]
B=[1,2,3,7,4,6,5,8,9]</p>

<p>I have to compare these two lists and return the count of no. of location in which items are differing using one line python code.</p>

<p>For example: 
the output should be 4 for given arrays because at index (3,4,5,6) the items are differing.So, program should return 4.</p>

<p>My way of doing this is comparing each and every location using for loop:</p>

<pre><code>count=0
for i in range(0,len(A)):
   if(A[i]==B[i]):
     continue
   else:
     count+=1
print(count)
</code></pre>

<p>Please help me in writing one line python code for this.</p>

<pre><code>count = sum(a != b for a, b in zip(A, B))
print(count)
</code></pre>

<p>or just <code>print sum(a != b for a, b in zip(A, B))</code></p>

<p>you can check about <a href="https://bradmontgomery.net/blog/pythons-zip-map-and-lambda/" rel="nofollow">zip/lambda/map here</a>, those tools are very powerfull and important in python..</p>

<p><a href="http://stackoverflow.com/questions/14050824/add-sum-of-values-of-two-lists-into-new-list">Here</a> you can also check others kind of ways to use those tools.</p>

<p>Have fun!!</p>

python arrays python-2.7 python-3.x numpy