I'm trying to increment multiple variables at the same time and stick it into one line. What would be the most pythonic way to do this if there is a way?
If you want to write a single line, you can try multiple assignment, but without the +=
syntax:
a, b, c = a+1, b+1, c+1
Or for a more pythonic solution, avoid the one-liner:
a += 1
b += 1
c += 1
Say you have
a, b, c = [1, 2, 3]
after you define:
def add1(x):
return x+1
You can do:
print(map(f,[a, b, c])) # prints [2, 3, 4]
which means that the following line will give you what you want:
a, b, c = map(add1,[a, b, c])
which is a bit easier to do than:
a, b, c = a+1, b+1, c+1
in case you have a big array. Further, you maintain readability and get your "one liner".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With