How to use the walrus operator in python

--

Did you know there is an operator called walrus introduced in python 3.8? I think most people, at least data scientists, don’t know about it and I really like, makes code much more legible. The operator looks like this:

:=

Let's see an example:

The difference might be small, but it can make much difference when the code is full of conditions. By the way, I believe it was first introduced in go, you will see it everywhere.

Here you have another small example I refactored recently:

There are many other use cases as you can see here https://realpython.com/python-walrus-operator/

--

--