Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Python Walrus Operator: Introduction, Example & Uses

python walrus operator
python walrus operator

Walrus Operator in Python 3.8. Need, example and List comprehension. The walrus operator” is a new method, using which you can assign values to a variable

What is Walrus Operator in Python

The Walrus operator in python was introduced in version 3.8 and is very useful to elegantly speed up code in specific scenarios. The := operator is called ‘walrus operator’ as it resembles the eyes and tusks of a walrus.

Pre-Requirements to use Walrus Operator

To use this new feature, you must have at least python 3.8 installed.
Below are instructions to install it in some popular operating systems –

  • Windows (32 bit) – download here and install it
  • Windows (64 bit) – download here and install it
  • Linux (Debian, Ubuntu & Linux Mint) – follow this post

Usage

Suppose we have the following code –

a = [3, 5, 7]
i = 0
while(i 

The above code is quite inefficient as it calculates the length of the list several times, at the start of every iteration of the loop. The most common way to speed up such code in long running loops is –

a = [3, 5, 7]
i = 0
l = len(a)
while(i 



The above code is a great solution, it calculates the length beforehand in a separate variable, however, the walrus operator saves us a line of code. The below solution uses the walrus operator :=

a = [3, 5, 7]
i = 0
while(i < (l := len(a))):
    print(i, l)
    i += 1

The above code uses the := operator to assign a value to l. Now, what is the difference between = and :=. Both assign value to a variable right?, Let’s see the difference by experimenting.

Let’s try to print the value of expression a = 1,

print(a=1)

When I try to run the above code, I get an error, the one below is a brief of what I get –

Traceback (most recent call last):
  File "", line 1, in 
TypeError: 'a' is an invalid keyword argument for print()

So, it seems, like the expression a = 1 is not printable. Probably, it does not return any printable value. It is a semantic error. Let’s try to print a := 1.

print(a:=1)

The above code prints 1. So, it seems the := operator when used to assign a value to an operator, returns the value assigned to it.

Note, := operator cannot be used as a statement. For example, we cannot use it like this

a := 1
print(a)

The above code produces, the error below –

Original exception was:
  File "", line 1
    a := 1
      ^
SyntaxError: invalid syntax

So, now we also know that we cannot use := for left most assignment. However, we can assign the value of 6 to both variables a and b, this way –

a = (b := 6)
print(a, b)

The above code prints “6 6“.

We can conclude, that the walrus operator needs to be used in an expression that must be a value to an operation.



This post first appeared on Technicalbud - Your Way To All Tech Solutions, please read the originial post: here

Share the post

Python Walrus Operator: Introduction, Example & Uses

×

Subscribe to Technicalbud - Your Way To All Tech Solutions

Get updates delivered right to your inbox!

Thank you for your subscription

×