Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 934
Dave Avatar asked May 13 '26 13:05

Dave


2 Answers

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.

like image 138
faressalem Avatar answered May 16 '26 04:05

faressalem


The best option :


list_of_elements =\
[
'AA',
'BB',
'CC',
'DD',
'EE',
'FF', 
'GG'
]
like image 21
Yaroslav_A Avatar answered May 16 '26 04:05

Yaroslav_A



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!