-3

Is putting the { next to the variable declaration required by syntax (for a dict variable) or is there a way to put it on the next line because if I do it like this I have to go through each item and check them until I find the item I want to change:

var = {thingy1: 1, thingy2: 2, thingy3: 3, thingy4: 4, thingy5: 5}

I would like to do it like this:

var =
{
    thingy1: 1,
    thingy2: 2
}

I have seen some people doing it like this but I just want to know if putting the { next to the variable declaration is required by syntax or not:

var = {
    thingy1: 1,
    thingy2: 2
}

(sorry if its a stupid question im a bad programmer and ive just started learning python lol)

  • 4
    Why not [try](https://i.stack.imgur.com/gL2XH.png) it yourself to [find out](https://i.stack.imgur.com/DHlhG.png)? – Pranav Hosangadi Apr 14 '21 at 15:13
  • 2
    Does this answer your question? [How can I do a line break (line continuation) in Python?](https://stackoverflow.com/questions/53162/how-can-i-do-a-line-break-line-continuation-in-python) – Brian61354270 Apr 14 '21 at 15:15
  • 2
    Please be aware that having many downvoted and deleted questions may result in a question ban. Consider to [edit] any [existing questions](https://stackoverflow.com/questions/67092783/how-do-you-make-a-multi-line-dict-variable-without-making-it-look-ugly) into shape instead of re-asking them. – MisterMiyagi Apr 14 '21 at 15:15

1 Answers1

0

You should always try it yourself first:

In [7]: var =                                                                   
  File "<ipython-input-7-3c8c4b28ffa8>", line 1
    var =
         ^
SyntaxError: invalid syntax


In [8]: var = \ 
   ...: { 
   ...:     "thing": "thing" 
   ...: }                                                                       

In [9]: var                                                                     
Out[9]: {'thing': 'thing'}

To answer your question, it's a SyntaxError to have nothing after the equal sign on a variable assignment. If you have some reason to continue the line you can use a \ backslash as I've shown in the above example.

By convention you put them on the same line unless the line would be too long (line continuation techniques are also described in this link):

var = {"thing1": 1, "thing2": 2}

If the line would be very long (e.g. hard to read), you'd do it the vertical way:

var = {
    "thing1": 1,
    "thing2": 2,
    ...
    "thing50": 50,
}
dephekt
  • 446
  • 2
  • 9