Home > Net >  Remove multiple elements from list using regex matching
Remove multiple elements from list using regex matching

Time:06-17

I have an excel workbook that has sheet names as Countries. Along with those sheets, there are additional sheets that I do not want to process. Hence I am trying to eliminate those sheets and keep only the sheets where sheet names are country names.

Below is the code I am using and it is getting the job done:

all_sheets = xl.sheet_names  # see all sheet names

entries_to_remove_1 = ['Information', 'Summary', 'Template', 'Source', 'BOW']
entries_to_remove_2 = list(filter(lambda x: 'Annual' in x, all_sheets))
entries_to_remove_3 = list(filter(lambda x: 'Quarterly' in x, all_sheets))
entries_to_remove_4 = list(filter(lambda x: 'WIP' in x, all_sheets))

entries_to_remove = entries_to_remove_1   entries_to_remove_2   entries_to_remove_3   entries_to_remove_4

entries_for_replacement = [i for i in all_sheets if i not in entries_to_remove]
all_sheets = list(set(entries_for_replacement))
all_sheets

Output:

['India', 'Brazil', 'Czech', 'Italy', 'Hungary', 'Poland']

Since I am using 4 statements just to gather the names of the sheets to be removed, I was hoping to find a more concise way to do the same task.

CodePudding user response:

It seems fine what you have in my opinion. Another option is to check for each sheet name if this name contains one of your words. If not, keep it in a list:

words = ['Information', 'Summary', 'Template', 'Source', 'BOW', 'Annual', 'Quarterly', 'WIP']
all_sheets = [sheet for sheet in all_sheets if all([word not in sheet for word in words])]
  • Related