5

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:'
    ]
rob
  • 71
  • 1
  • 1
  • 3
  • 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
  • possible duplicate of [How would you know if you've written readable and easily maintainable code?](http://programmers.stackexchange.com/questions/141005/how-would-you-know-if-youve-written-readable-and-easily-maintainable-code) – gnat Jun 10 '15 at 18:31
  • @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
  • http://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 03 '15 at 02:26

2 Answers2

8

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).

Joe Reid
  • 121
  • 3
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.

ErayAydin
  • 31
  • 1