-1

I'm trying to extract the elements of a list to turn it into a CSV file. I have a long list containing string elements. Example:

print(list_x[0])
['NAME\tGill Pizza Ltd v Labour Inspector', 'JUDGE(S)\tWilliam Young, Ellen France & Williams JJ', 'COURT\tSupreme Court', 'FILE NUMBER\tSC 67-2021', 'JUDG DATE\t12 August 2021', 'CITATION\t[2021] NZSC 97', 'FULL TEXT\t PDF judgment', 'PARTIES\tGill Pizza Ltd (First Applicant), Sandeep Singh (Second Applicant), Jatinder Singh (Third Applicant/Fourth Applicant), Mandeep Singh (Fourth Applicant/Third Applicant), A Labour Inspector (Ministry of Business, Innovation and Employment) (Respondent), Malotia Ltd (First Applicant)', 'STATUTES\tEmployment Relations Act 2000 s6(5), s228(1)', 'CASES CITED\tA Labour Inspector (Ministry of Business, Innovation and Employment) v Gill Pizza Ltd [2021] NZCA 192', 'REPRESENTATION\tGG Ballara, SP Radcliffe, JC Catran, HTN Fong', 'PAGES\t2 p', 'LOCATION\tNew Zealand Law Society Library', 'DATE ADDED\tAugust 19, 2021']

Is it possible to do something like:

name_list = []
file_number_list = []
subject_list = []
held_list = []
pages_list = []
date_list = []

for i in range(len(list_x)):
  if list_x[i].startswith('NAME'):
      name_list.append(list_x[i])
  elif list_x[i].startswith('FILE NUMBER'):
      file_number_list .append(list_x[i])
  elif list_x[i].startswith('SUBJECT'):
      subject_list .append(list_x[i])
  elif list_x[i].startswith('HELD'):
      held_list .append(list_x[i])
  elif list_x[i].startswith('PAGES'):
      pages_list .append(list_x[i])
  elif list_x[i].startswith('DATE ADDED'):
      date_list .append(list_x[i])

Any help is appreciated. Cheers.

Lui Hellesoe
  • 185
  • 1
  • 10
  • 3
    Get out of the habit of using `for i in range(len(list)):` Use `for item in list:` – Barmar Aug 31 '21 at 05:41
  • 1
    Yes, what you've written should work. Are you having a problem with it? – Barmar Aug 31 '21 at 05:42
  • In general, keeping related data in separate lists makes things harder. I suggest you make a list of dictionaries, with all the related data together in a dictionary. – Barmar Aug 31 '21 at 05:43
  • `list_of_dicts = [dict(item.split('\t') for item in s) for s in list_x]` – Barmar Aug 31 '21 at 05:45
  • `for i in range(len(list)):` -> `for i, value in enumerate(list):` – 0dminnimda Aug 31 '21 at 05:56
  • Yes for me it is not working but but I found this SO question which helps: https://stackoverflow.com/questions/12783705/if-any-item-of-list-starts-with-string – Lui Hellesoe Aug 31 '21 at 05:57

1 Answers1

3

You can also try something like this:

my_dict_list = []
for item in list_x:
   my_dict_list.append(dict(zip([i.split('\t', 1)[0] for i in item], [i.split('\t', 1)[1] for i in item])))

Results:

[{'NAME': 'Gill Pizza Ltd v Labour Inspector',
 'JUDGE(S)': 'William Young, Ellen France & Williams JJ',
 'COURT': 'Supreme Court',
 'FILE NUMBER': 'SC 67-2021',
 'JUDG DATE': '12 August 2021',
 'CITATION': '[2021] NZSC 97',
 'FULL TEXT': ' PDF judgment',
 'PARTIES': 'Gill Pizza Ltd (First Applicant), Sandeep Singh (Second Applicant), Jatinder Singh (Third Applicant/Fourth Applicant), Mandeep Singh (Fourth Applicant/Third Applicant), A Labour Inspector (Ministry of Business, Innovation and Employment) (Respondent), Malotia Ltd (First Applicant)',
 'STATUTES': 'Employment Relations Act 2000 s6(5), s228(1)',
 'CASES CITED': 'A Labour Inspector (Ministry of Business, Innovation and Employment) v Gill Pizza Ltd [2021] NZCA 192',
 'REPRESENTATION': 'GG Ballara, SP Radcliffe, JC Catran, HTN Fong',
 'PAGES': '2 p',
 'LOCATION': 'New Zealand Law Society Library',
 'DATE ADDED': 'August 19, 2021'},
{....
 ....
 ...}]
BlackMath
  • 1,708
  • 1
  • 11
  • 14