Search code examples
pythoncode-formatting

Python split list into several lines of code


I have a list in Python which includes up to 50 elements. In order for me to easily add/subtract elements, I'd prefer to either code it vertically (each list element on one Python code line) or alternatively, import a separate CSV file?

list_of_elements = ['AA','BB','CC','DD','EE','FF', 'GG']

        for i in list_of_elements:
        more code...

I'd prefer code like this:

list_of_elements = 
['AA',
'BB',
'CC',
'DD',
'EE',
'FF', 
'GG']
    
        for i in list_of_elements:
        more code...

Just to clarify, it's not about printing, but about coding. I need to have a better visual overview of all the list elements inside the Python code.


Solution

  • The first line should contain the first element, like this:

    list_of_elements = ['AA',
    'BB',
    'CC',
    'DD',
    'EE',
    'FF', 
    'GG']
    

    or as Naufan Rusyda Faikar commented: Put backslash next to = Or put the left bracket next to =.

    list_of_elements = \
    ['AA',
    'BB',
    'CC',
    'DD',
    'EE',
    'FF', 
    'GG']
    
    list_of_elements = [
    'AA',
    'BB',
    'CC',
    'DD',
    'EE',
    'FF', 
    'GG']
    

    All three will work.