LeetCampus
Interview Question

What is Walrus Operator?

July 24, 2025
1 view
Difficulty: Medium
Popularity: Common
Share on

Question Explanation

The Walrus Operator (:=) is a relatively new feature introduced in Python 3.8 that allows you to assign values to variables as part of an expression. This operator enables you to both assign and return a value in a single line, which can help reduce redundancy in your code. Interviewers ask about this operator to gauge your understanding of modern Python features and your ability to write concise, efficient code. Understanding the Walrus Operator can lead to cleaner code, especially in cases like list comprehensions or while loops where you may want to both evaluate and assign a variable. Common misconceptions include thinking that it replaces traditional assignment or that it is only useful in specific scenarios. In reality, it can be applied in various contexts, improving code readability and efficiency. Additionally, being familiar with this operator can signal to interviewers that you are keeping up with the evolving Python landscape, which is essential for any Python developer.

Sample Answers

Example 1: Simplifying List Comprehensions

The Walrus Operator can significantly simplify list comprehensions. For example, consider we want to create a list of lengths of words in a sentence. Traditionally, you might write:

words = ['Python', 'is', 'awesome']
lengths = [len(word) for word in words]

Using the Walrus Operator, you can streamline this:

lengths = [l := len(word) for word in words if l > 3]

Here, l is assigned the length of word, and if it's greater than 3, it gets included in the list. This reduces the need for a separate line to calculate lengths and makes the comprehension more concise and readable.

Example 2: Enhancing While Loops

The Walrus Operator is particularly useful in while loops, allowing you to assign and evaluate in a single statement. For instance, if you're reading lines from a file until you reach an empty line:

with open('file.txt') as f:
    while (line := f.readline().strip()):
        print(line)

Instead of first assigning line and then checking its truthiness, the Walrus Operator allows you to do both in one line. This not only makes the code cleaner but also reduces the lines of code, enhancing readability and efficiency.

Example 3: Conditional Expressions

Another use case is in conditional expressions where you want to perform an assignment based on a condition. For example, if you want to compute and print a value only if it meets a certain condition, you might do:

if (n := input('Enter a number: ')).isdigit():
    print(f'The square is: {int(n) ** 2}')

Here, n is assigned the input directly within the if statement, allowing you to both validate and use the input in one go. This usage highlights the operator's ability to improve flow and reduce redundancy in your code.

Keywords

PythonWalrus OperatorProgrammingCode EfficiencyAssignment Expressions

Ready to practice more questions?

Explore our collection of technical interview questions from top companies.

View All Questions