Skip to content

Walrus operator: assign on the fly 🛫

I recently discovered that Python has a neat operator for assigning a value and using it immediately: the := operator, also called the walrus operator.

For example, instead of:

value = data.get("key")
if value:
    print(len(value))

You can do:

if (value := data.get("key")):
    print(len(value))

Boom 💥 value is assigned and ready to roll.

💡 Takeaway: := = assign inline, use instantly, keep it neat.