How to separate the strings inside a list of strings according to the appearance of certain characters inside the strings?

Question:

import re #to use matching regex inside list elements

separator_elements_list = ["'", """, "¿", "?", "¡", "!", "(", ")", "[", "]", "{", "}", ";", ",", ".", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "&", "#", "$", "=", "+", "-", "*", "/", "~", " "]

list_verbs_in_this_input = ['llegamos  ', "allí')hacía", "/allá en el    centro. habrá"]  #input_list

evaluates_if_substring_is_a_verb_func(input_substring) # --> print here to check the result

I need to separate the elements of the list_verbs_in_this_input list if any of the separator symbols indicated in the separator_elements_list list appear, and also eliminate the empty strings or those that only contain whitespace.

This is how the list should look after filtering:

['llegamos', "allí", "hacía", "allá", "en", "el", "centro", "habrá"]
Asked By: Matt095

||

Answers:

You can split on the separators, then trim each part.

import re
pattern = "|".join(re.escape(sep) for sep in separator_elements_list)
res = [part.strip() for x in list_verbs_in_this_input 
           for part in re.split(pattern, x) if part]
Answered By: Unmitigated