0

I want to do something like:

a,b,c,d = 1,2,3,4
a,b,c,d += 2,4,6,8

But this does not work. I know I can increase them individually but I thought there would be a simpler way. The only alternative I came up with was this ugly list comprehension:

a,b,c,d = [j+k for idxj,j in enumerate((a,b,c,d)) for idxk,k in enumerate((2,4,6,8)) if idxj==idxk]

Is there a better way?

James
  • 387
  • 1
  • 11
  • 1
    Hi Jimi: I like the syntax that you are proposing ( that is, a,b,c,d += 2,4,6,8 ). It's terse and easy to read. Of course, the correct one-liner that you provided is indeed ugly. Let's translate ugly ( which is accurate ) to something more descriptive: difficult to maintain. I think that if you were dealing with only 4 variables, you might as well increase them individually -- because it's easy to read. If you had a lot of variables, you might consider using some collection ( depending on whatever your program requires ). In a nutshell, keep it simple. – Mark Mar 27 '17 at 16:37

1 Answers1

3

zip, generally:

a, b, c, d = [x + y for x, y in zip((a, b, c, d), (2, 4, 6, 8))]

but there’s also our friend the semicolon:

a += 2; b += 4; c += 6; d += 8

Replace with a newline at your discretion.

Ry-
  • 218,210
  • 55
  • 464
  • 476