1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] walrus operator in dict comprehension

Discussão em 'Python' iniciado por Stack, Novembro 23, 2025.

  1. Stack

    Stack Membro Participativo

    I wanted to avoid double evaluation of a mean in a dict comprehension, and I tried using the walrus operator:

    >>> dic = {"A": [45,58,75], "B": [55,82,80,92], "C": [78,95,90], "D":[98,75]}
    >>> q = {x: (mean := (sum(dic[x]) // len(dic[x]))) for x in dic if mean > 65}


    but this gave me the following error:

    Traceback (most recent call last):
    File "<pyshell#2>", line 1, in <module>
    q = {x: (mean := (sum(dic[x]) // len(dic[x]))) for x in dic if mean > 65}
    File "<pyshell#2>", line 1, in <dictcomp>
    q = {x: (mean := (sum(dic[x]) // len(dic[x]))) for x in dic if mean > 65}
    NameError: name 'mean' is not defined


    This error only happens when I try to use the variable, no problem while defining it:

    >>> q = {x: (mean := (sum(dic[x]) // len(dic[x]))) for x in dic if (sum(dic[x]) // len(dic[x])) > 65}
    >>> mean
    86
    >>> q
    {'B': 77, 'C': 87, 'D': 86}


    Why? Where did I get it wrong?

    Continue reading...

Compartilhe esta Página