pythonsyntax

Do dictionaries have the same implicit line continuation as parentheses?


I was surprised that this seems to work without parentheses:

dict = {
    "a": 1 if True
        else 2,
    "b": 2
}

I know that dictionaries have implicit line continuation, but I presumed that only let you split between commas. Does it simply function like parentheses, where everything is treated as one line?


Solution

  • Yes, implicit line continuation/joining takes place within parentheses, square brackets and curly braces in Python as documented:

    Expressions in parentheses, square brackets or curly braces can be split over more than one physical line without using backslashes.