advanced patterns with Python 3.10's walrus operator in list comprehensions
I can't seem to get I'm following best practices but I tried several approaches but none seem to work..... I'm experiencing unexpected behavior when using the walrus operator `:=` in list comprehensions in Python 3.10. I wanted to create a list of squares for even numbers from 0 to 10, and I'm trying to filter and compute them in one go. Here's what I wrote: ```python squares = [y := x * x for x in range(11) if y % 2 == 0] ``` However, when I run this code, I get an `UnboundLocalError` stating that the local variable 'y' is referenced before assignment. I expected that the walrus operator would allow me to assign `y` within the list comprehension and use it in the filtering condition. I also tried breaking it down into separate steps: ```python squares = [] for x in range(11): if (y := x * x) % 2 == 0: squares.append(y) ``` This works fine, but it's not as elegant as I'd like. I'm curious if there's a specific limitation or quirk with how list comprehensions handle variable scope when using the walrus operator. Could anyone help clarify this behavior? How can I properly use the walrus operator in a list comprehension for this scenario? My development environment is Linux. Any help would be greatly appreciated! This issue appeared after updating to Python LTS. Cheers for any assistance!