Why does zip change my lists?
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Island
--
Chapters
00:00 Why Does Zip Change My Lists?
00:34 Accepted Answer Score 18
01:20 Answer 2 Score 2
01:43 Thank you
--
Full question
https://stackoverflow.com/questions/3997...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #list #python27 #listcomprehension
#avk47
ACCEPTED ANSWER
Score 18
zip did not change your list. You lost the initial references to your lists when you assigned the name a and b to the loop variables in:
for a, b in zip(a,b):
#   ^  ^
A simple fix is to change those names to say, i and j:
for i, j in zip(a,b):
One thing to bear in mind when using Python is that names are bound to objects, and therefore can be unbound or even rebound. No name is for keeps. Once you change the object a name is referencing like you did, the name starts to reference the new object.
On another note, for loops assign the objects from the iterable to the name(s) provided, similar to a regular assignment, but with repetitions. So the values you get for a and b at the end of the for loop are those of the last assignment done in the last iteration.
Do bear these in mind.
ANSWER 2
Score 2
Suppose a is a list, and you write a = a[0]. Now you'd expect a to be not a list, but the first value in the list.
Similarly, when you write for a,b in zip(a,b) you re-assign a and b to hold the first element in each iterable. Try:
for x,y in zip(a,b):
  if x==1 and y==1:
      print "cool"
print a,b