0

I thought this should work, but it didn't (yields 0):

n1 = [1, 2, 3, 4]
n2 = [5, 6, 7, 8]

pair = zip(n1, n2)

dif = sum(abs(v1 - v2) for v1, v2 in pair)
print(dif)

But neglecting my pair variable and using the code directly worked just fine (yields 16):

dif = sum(abs(v1 - v2) for v1, v2 in zip(n1, n2))

Shouldn't both of them yield the same answer?

double-beep
  • 5,031
  • 17
  • 33
  • 41

1 Answers1

0

The first approach is supposed to work. If I was to guess why it's not, maybe you've already iterated over the 'pair' variable before you used it in your generator expression

Seth250
  • 16
  • 4
  • Yea, I thought the same, probably the generator has already been used before the expression – dome May 22 '21 at 20:49
  • You are right guys, thank you for helping me out. I am simply stupid... I used a print statement `print(*pair)` in order to check the structure. I did not know that this could have implications. Thanks again! :) @Seth250, @dome – Julian Kirsch May 22 '21 at 21:17