A cleaner version of the cleanup list can be implemented using recursion. This will allow you to have an infinite number of lists within a list, while maintaining very low complexity for your code.
Note: this also allows for security checks to avoid data type problems with the strip. This allows your list to contain int, float, and more.
def clean_list(list_item): if isinstance(list_item, list): for index in range(len(list_item)): if isinstance(list_item[index], list): list_item[index] = clean_list(list_item[index]) if not isinstance(list_item[index], (int, tuple, float, list)): list_item[index] = list_item[index].strip() return list_item
Then just call the function with your list. All values will be cleared inside the list.
clean_list (network)
source share