0

I am wondering if this is possible somehow. I would like to increase both values once that I unpack it, without having to create two temps variables. Is this somehow possible with multiple assignment and unpacking? I started to try out here:

a, b = 1, 1                                                                                                                                                                           
for i in [1,2,3]: 
  a, b += 1, 1                                                                                                                                                                      

File "<ipython-input-2-a1e761cf8ae1>", line 2
  a, b += 1, 1
  ^
SyntaxError: illegal expression for augmented assignment
for i in [1,2,3]: 
  a, b += (1, 1)                                                                                                                                                                    

File "<ipython-input-3-9317a6e51de4>", line 2
  a, b += (1, 1)
  ^
SyntaxError: illegal expression for augmented assignment
for i in [1,2,3]: 
  (a, b) += (1, 1)                                                                                                                                                                  

File "<ipython-input-4-446db199ce6f>", line 2
  (a, b) += (1, 1)
   ^
SyntaxError: illegal expression for augmented assignment
for i in [1,2,3]: 
  a += 1 
Torxed
  • 22,866
  • 14
  • 82
  • 131
may
  • 1,073
  • 4
  • 14
  • 31

2 Answers2

1

maybe map can help you !

a,b =1,1
a,b = map(lambda x,y:x+y, [a,b],[1,1])

map() function returns a map object of the results after applying the given function to each item of a given iterable (list, tuple etc.)

chseng
  • 116
  • 4
0

Not exactly what you want, but maybe helpful. Numpy can easily add the values just before you unpack.

import numpy as np

ar = np.array([1,1])
ar +=  1
a,b = ar

yields:

(2,2)
sophros
  • 14,672
  • 11
  • 46
  • 75
Christian Sloper
  • 7,440
  • 3
  • 15
  • 28