0

I have tried to use list comprehension but I just can't figure out how to structure it for my particular issue. I have a nested list. The sublist is at the end:

['x', '', 'x', 'y', 'x', Suite 111', 'Prairie Village', 'KS', '\t66207', '5200 W. 94th Terrace, Suite 111', 'Prairie Village', 'KS', '\t66207', 'x', 'y', 'x', 'y', 'Kansas City', 'SLBE', 'This company provides Framing and Finish Carpentry installation services, Drywall & Acoustical Ceilings installation services and Construction Management services for Commercial & Institutional Buildings construction projects. . Metal Studs - Trim - Cabinetry - Carpenters - Doors - Windows - Hardware - Sheet Rock - Suspended ceilings - Construction Administration - Project Management - Tenant Finish - Interior Finish', '', ['236220 - Construction management, commercial and institutional building', '236220 - Project Management (Inactive effective 02-12-2020)', '238130 - Framing contractors', '238130 - Stud wall (e.g., wood, steel) installation', '238130 - Carpentry', '238310 - Acoustical ceiling tile and panel installation', '238310 - Ceiling tile installation', '238310 - Drywall contractors', '238310 - Drywall hanging', '238350 - Cabinetry work performed at the construction site', '238350 - Door and window, prefabricated, installation', '238350 - Trim and finish carpentry contractors', '238350 - Finish Carpentry Contractors']]

I would like to go through and remove the entire element from the list which contains the substring 'Inactive', however I cannot figure out how. I'm thinking that I could use regex but I'm just confused on how I would go about doing this.

My desired outcome is:

['x', '', 'x', 'y', 'x', Suite 111', 'Prairie Village', 'KS', '\t66207', '5200 W. 94th Terrace, Suite 111', 'Prairie Village', 'KS', '\t66207', 'x', 'y', 'x', 'y', 'Kansas City', 'SLBE', 'This company provides Framing and Finish Carpentry installation services, Drywall & Acoustical Ceilings installation services and Construction Management services for Commercial & Institutional Buildings construction projects. . Metal Studs - Trim - Cabinetry - Carpenters - Doors - Windows - Hardware - Sheet Rock - Suspended ceilings - Construction Administration - Project Management - Tenant Finish - Interior Finish', '', ['236220 - Construction management, commercial and institutional building', '238130 - Framing contractors', '238130 - Stud wall (e.g., wood, steel) installation', '238130 - Carpentry', '238310 - Acoustical ceiling tile and panel installation', '238310 - Ceiling tile installation', '238310 - Drywall contractors', '238310 - Drywall hanging', '238350 - Cabinetry work performed at the construction site', '238350 - Door and window, prefabricated, installation', '238350 - Trim and finish carpentry contractors', '238350 - Finish Carpentry Contractors']]

Would somebody please help me?

  • Do you just want to remove the string with the substring 'inactive'? Or also the sublist it is part of? – whackamadoodle3000 Jun 05 '20 at 18:52
  • I would like to remove the element in the sublist. If a string contains the keyboard 'Inactive', I would like to remove that element of the list entirely, but keep the others in the list that don't mention 'Inactive'. So yes to remove the entire string with the substring inactive. – collinj153 Jun 05 '20 at 18:55
  • @Sayse the question you linked does not include the case of the list being nested – whackamadoodle3000 Jun 05 '20 at 18:59
  • @whackamadoodle3000 - If that is the part the op is struggling with then they should edit their question to include what they've tried / researched. The duplicate matches the question in the op's title. – Sayse Jun 05 '20 at 19:01
  • @Sayse I've updated it. Sorry! I'm very new to posting here. – collinj153 Jun 05 '20 at 19:03
  • You could use this list comprehension: `[e if not isinstance(e, list) and not 'Inactive' in e else [a if not 'Inactive' in a for a in e] for e in nestedlist]` – whackamadoodle3000 Jun 05 '20 at 19:08
  • I was having a hard time understanding how to use list comprehension when I was researching this before I posted. How would I implement this? Would I set my list to be equal to your statement? or loop through my list and set each element at x to be the result of that statement? – collinj153 Jun 05 '20 at 19:13
  • All you need to do is set `[e if not isinstance(e, list) and not 'Inactive' in e else [a if not 'Inactive' in a for a in e] for e in nestedlist]` equal to your new list variable. nestedlist is your original list that you are trying to remove the inactives from, and the list comprehension returns your new list that you would put into a variable – whackamadoodle3000 Jun 05 '20 at 19:31
  • I'm sorry to keep bugging you, this is what I have but it gives me an invalid syntax error at that line. newList = [e if not isinstance(e, list) and not 'Inactive' in e else [a if not 'Inactive' in a for a in e] for e in rows] – collinj153 Jun 05 '20 at 19:39
  • I have no idea how to comment code^. rows is the name of the original list. – collinj153 Jun 05 '20 at 19:40
  • I'm sorry, I didn't test the code, there were a few bugs. Here is the updated: `newList = [e if not isinstance(e, list) else [a for a in e if not 'Inactive' in a] for e in rows if isinstance(e, list) or not 'Inactive' in e]` – whackamadoodle3000 Jun 05 '20 at 20:51

0 Answers0