+= with multiple variables in python

11,279

Solution 1

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

Solution 2

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".

Share:
11,279

Related videos on Youtube

user3255511
Author by

user3255511

Updated on September 15, 2022

Comments

  • user3255511
    user3255511 over 1 year

    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?

    • Matt Ball
      Matt Ball about 10 years
      The Pythonic way is to not do it on one line.
    • Tony Hopkinson
      Tony Hopkinson about 10 years
      The any language at all ever way is not to do it all one one line, with the exception of Perl, of course. :)
  • user3255511
    user3255511 about 10 years
    I like my code in one liners I'm starting to think this might actually just be the simplest way.
  • markcial
    markcial about 10 years
    @user3255511 you will have a lot of mortal enemies in the future if you don't format nicely your code, xD
  • user3255511
    user3255511 about 10 years
    This requires casting it into a list and that makes me worry about more complexity but useful information. It gives me ideas on how to finally make use of the lambda function.
  • Nir Alfasi
    Nir Alfasi about 10 years
    @user3255511 of course, you can define the lambda "inline" (inside the map)
  • Burhan Khalid
    Burhan Khalid about 10 years
    You will also curse yourself if you ever have to maintain your own code in ... oh ... 2 months later. I bet you also don't like comments.
  • Adi Inbar
    Adi Inbar about 10 years
    @user3255511 Not trying to preach here...I know "religious" debates are out of place at SO and I'm not trying to start one about which is "better", but if as you say you prefer compact syntax that crams multiple actions into a single line, you'd probably be more comfortable with Perl. Matt Ball's comment sums it up nicely. On the flip side, the Perlish way is to do it all on one line along with at least two or three other things.
  • Air
    Air almost 10 years
    The two examples provided here are not equivalent. The former provides simultaneous updates, which is part of the OP's request, but the latter does not.
  • Óscar López
    Óscar López almost 10 years
    @AirThomas didn't you read the clarification? avoid the one liner, it's clearer that way, hence more Pythonic.
  • Air
    Air almost 10 years
    It doesn't matter how Pythonic the multi-line version is, if a chance exception thrown between one line and the next will cause the entire program to fail. And that can be a difficult bug to anticipate. Imagine you are writing this answer for someone who next year will be writing a guidance module for a $125 million Mars orbiter...