Why does zip change my lists?
<p>I noticed a (to me) very strange behavior, I don't understand:
I got a list and an numpy array both with binary values and I want to check the true positives (both==1 at the same time).</p>

<pre><code>import numpy as np
a = [0,0,1,1]
b = np.array([1,0,1,0])

for a,b in zip(a,b):
  if a==1 and b==1:
      print "cool"
print a,b
</code></pre>

<p>Now the craziness begins. a and b are not longer a list or a numpy array but an integer and numpy float? How on earth did that happen?
Thanks for your help!</p>

<p>Suppose <code>a</code> is a list, and you write <code>a = a[0]</code>. Now you'd expect <code>a</code> to be not a list, but the first value in the list.</p>

<p>Similarly, when you write <code>for a,b in zip(a,b)</code> you re-assign <code>a</code> and <code>b</code> to hold the first element in each iterable. Try:</p>

<pre><code>for x,y in zip(a,b):
  if x==1 and y==1:
      print "cool"
print a,b
</code></pre>

python list python-2.7 list-comprehension