Search code examples
pythonlist-comprehensiondictionary-comprehension

single line if else in python dictionary comprehension method


I just tried a list comprehension like this

[i if i==0 else i+100 for i in range(0,3)]

and it worked, but when I tried a similar dict comprehension, it throws an error:

d={3:3}
{d[i]:0 if i==3 else d[i]:True for i in range(0,4) }

What could be the reason? How can I use dict comprehension using if else?

The error this produces:

    {d[i]:0 if i==3 else d[i]:True for i in range(0,4) }
                             ^
SyntaxError: invalid syntax

Note: The example I used here is just a random one, not my actual code. I can do this with alternative an solution, but I'm only looking at dict comprehensions now to learn.


Solution

  • You are using a conditional expression. It can only be used in places that accept expressions.

    In a dictionary comprehension, the key and value parts are separate expressions, separated by the : (so the : character is not part of the expressions). You can use a conditional expression in each one of these, but you can't use one for both.

    You only need to use it in the value part here:

    {d[i]: 0 if i == 3 else True for i in range(4)}
    

    However, you'll get a KeyError exception because the d dictionary has no 0, 1, and 2 keys.

    See the Dictionary displays section of the expression reference documentation:

    dict_display       ::=  “{” [key_datum_list | dict_comprehension] “}”
    [...]
    dict_comprehension ::=  expression “:” expression comp_for
    

    [...]

    A dict comprehension, in contrast to list and set comprehensions, needs two expressions separated with a colon followed by the usual “for” and “if” clauses.