3

when coding a very long list in python, is it better to fit several items on each line or should I limit it to 1 per line? 99% of the time I would go with style2 below but I have 5 lists each about the same length as the one below and it seems like too many lines. any advice would be appreciated.

for example:

style1 = [
    'Email:', 'SSN:', 'Address:', 'Home Phone:',
    'Mobile Phone: ', 'DOB:', 'Date of Surgery:',
    'Date of Service:', 'Facility of Service:', 'Clinic Number:',
    'Employer:', 'Work Phone: ', 'Fax: ', 'Type:', 'IPA:',
    'Health Plan:', 'ID #:', 'Claims Address:', 'Group #:',
    'Claim # / PO #:', 'Phone:', 'Fax:', 'Contact',
    'Adjuster Email', 'Util Review Phone', 'Util Review Fax',
    'Doctor:', 'NPI #: ', 'Date of Injury: ', 'Body Parts:',
    'Body Part Side:', 'Gender:', 'Diagnosis:', 'Diagnosis 2:',
    'Procedure:'
    ]

style2 = [
    'Email:',
    'SSN:',
    'Address:',
    'Home Phone:',
    'Mobile Phone: ',
    'DOB:',
    'Date of Surgery:',
    'Date of Service:',
    'Facility of Service:',
    'Clinic Number:',
    'Employer:',
    'Work Phone: ',
    'Fax: ',
    'Type:',
    'IPA:',
    'Health Plan:',
    'ID #:',
    'Claims Address:',
    'Group #:',
    'Claim # / PO #:',
    'Phone:',
    'Fax:',
    'Contact',
    'Adjuster Email',
    'Util Review Phone',
    'Util Review Fax',
    'Doctor:',
    'NPI #: ',
    'Date of Injury: ',
    'Body Parts:',
    'Body Part Side:',
    'Gender:',
    'Diagnosis:',
    'Diagnosis 2:',
    'Procedure:'
    ]

closed as primarily opinion-based by Dan Pichelman, durron597, Ixrec, gnat, user22815 Jun 10 '15 at 21:40

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.

  • Your example looks like breaking the list into subgroups would be appropriate, e.g. [['DOB:', ...], [], []]. It's nowhere near long enough for the cost of flattening to be an issue. – Ixrec Jun 10 '15 at 12:37
  • @gnat I don't agree that this is a duplicate. That question is a general question about determining whether your own code is readable, and is language agnostic. My question is specific to one situation in one language, very long lists in python. – rob Jun 10 '15 at 19:18
  • meta.stackexchange.com/a/194495/165773 – gnat Jun 10 '15 at 19:39
  • When you get a lot of data, I prefer to move it to an external file, so open('file_name.ext', 'r') style3 = f.readlines() – acutesoftware Jul 3 '15 at 2:26
4

The PEP 8 Python style guide shows lists in the form:

my_list = [
    1, 2, 3,
    4, 5, 6,
    ]

In the indentation section of the pep https://www.python.org/dev/peps/pep-0008/#indentation

For this reason I would say style 1 (ensuring not to violate the line length rules).

3

Personally, I'm using style2. Because its very easy for adding new item to list. Also with style2 you can easly delete item with deleting row. But its difficult on style1.

Not the answer you're looking for? Browse other questions tagged or ask your own question.