Python 3.8 is still in development. But many alpha versions have been released. One of the latest features in Python 3.8 is the Walrus Operator. In this article, we’re going to discuss the Walrus operator and explain it with an example.
Introduction
Walrus-operator is another name for assignment expressions. According to the official documentation, it is a way to assign to variables within an expression using the notation NAME := expr. The Assignment expressions allow a value to be assigned to a variable, even a variable that doesn’t exist yet, in the context of expression rather than as a stand-alone statement.
Code :
a = [1, 2, 3, 4] if (n := len(a)) > 3: print(f"List is too long ({n} elements, expected <= 3)") |
Output :

Here’s instead of using “len(a)” in two places we have assigned it to a variable called “n”, which can be used later. This helps us to resolve code duplication and improves readability.
Example –
Let’s try to understand Assignment Expressions more clearly with the help of an example using both Python 3.7 and Python 3.8. Here we have a list of dictionaries called “sample_data”, which contains the userId, name and a boolean called completed.
sample_data = [ {"userId": 1, "name": "rahul", "completed": False}, {"userId": 1, "name": "rohit", "completed": False}, {"userId": 1, "name": "ram", "completed": False}, {"userId": 1, "name": "ravan", "completed": True} ] print("With Python 3.8 Walrus Operator:") for entry in sample_data: if name := entry.get("name"): print(f'Found name: "{name}"') print("Without Walrus operator:") for entry in sample_data: name = entry.get("name") if name: print(f'Found name: "{name}"') |
Output:

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Benefits of Double Division Operator over Single Division Operator in Python
- Operator Functions in Python | Set 2
- Ternary Operator in Python
- Operator Functions in Python | Set 1
- Python | Operator.countOf
- Python | List comprehension vs * operator
- Check if a value exists in a DataFrame using in & not in operator in Python-Pandas
- Concatenate two strings using Operator Overloading in Python
- What is a modulo operator (%) in Python?
- Difference between == and is operator in Python
- Operator Overloading in Python
- Python - Star or Asterisk operator ( * )
- What does the Double Star operator mean in Python?
- Like Operator in SAS Programming
- Arrow operator -> in C/C++ with Examples
- Matrix operations using operator overloading
- New '=' Operator in Python3.8 f-string
- Modulo Operator (%) in C/C++ with Examples
- Important differences between Python 2.x and Python 3.x with examples
- Python | Set 4 (Dictionary, Keywords in Python)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.
