for loop returning unexpected values when squaring values
I have two lists that I want to square the following code is working
a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
b = [1, 3, -4, 5, 5, 3, 2, 1, 4, 8, 9]
a_sqrd = []
b_sqrd = []
for i in a:
a_sqrd.append(a[i]*a[i])
b_sqrd.append(b[i]*b[i])
print a_sqrd
print b_sqrd
result:
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
[1, 9, 16, 25, 25, 9, 4, 1, 16, 64, 81]
but when I try
a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
b = [1, 3, -4, 5, 5, 3, 2, 1, 4, 8, 9]
a_sqrd = []
b_sqrd = []
for i in a:
a_sqrd.append(a[i]*a[i])
for i in b:
b_sqrd.append(b[i]*b[i])
print a_sqrd
print b_sqrd
result:
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
[9, 25, 1, 9, 9, 25, 16, 9, 25, 16, 64]
why does running two for loops affect list b? Im expecting the same result
as in a, im sure there is something obvious im missing
No comments:
Post a Comment