Home > Net >  Python Update Value in one Line?
Python Update Value in one Line?

Time:08-27

In python I have:

c_f = None
#c_f may change here
if not c_f:
    c_f = new_c_f

Can I write last 2 lines in one line?

  1. If I want to keep first non-None value

  2. If I want to keep last non-None value

Please Note that doing:

c_f = c_f or new_c_f

solves one of the cases

CodePudding user response:

Assuming you're trying to compress

if not c_f:
    c_f = new_c_f

to one line, you can change it to a one-liner:

if not c_f: c_f = new_c_f
  • Related