0

I have a long snippet of code and i am trying to shorten it. This is what I have now:

statuses = cleaned_data.get("statuses")
if statuses:
    for status in statuses:
        ...

it would be nice if i can assign the value of cleaned_data.get("statuses") inside the if condition, like this:
if cleaned_data.get("statuses") as statuses
and carry on.
Is it doable in python?

1 Answers1

1

You can use Assignment Expression (a.k.a walrus operator):

if statuses := cleaned_data.get("statuses"):
    for status in statuses:
matszwecja
  • 6,357
  • 2
  • 10
  • 17