+ = with multiple variables in python

I am trying to simultaneously increase several variables and insert them into one line. What would be the most pythonic way to do this if there is a way?

+4
source share
2 answers

If you want to write a single line, you can try several assignments, but without syntax +=:

a, b, c = a+1, b+1, c+1

Or for a more pythonic solution, avoid a one-liner:

a += 1
b += 1
c += 1
+4
source

Say what you have

a, b, c = [1, 2, 3]

after determination:

def add1(x):
    return x+1

You can do:

print(map(f,[a, b, c])) # prints [2, 3, 4]

which means the following line will give you what you want:

a, b, c = map(add1,[a, b, c])

which is a little easier to do than:

a, b, c = a+1, b+1, c+1

if you have a large array. In addition, you maintain readability and get your “one liner”.

+1

Source: https://habr.com/ru/post/1524626/


All Articles