Skip to content
Advertisement

how to have re.sub work for multiple pattern replace on list of list?

I have a list of list input- old_list:

JavaScript

my desired output – new_list:

JavaScript

and I have tried

1.

JavaScript
JavaScript
JavaScript
JavaScript
  1. and replace(new_list, old, new) for ...

but none of them works, the output is the same as the original old_list. Any suggestions? Thanks!

Advertisement

Answer

  1. You need to use output of each iteration as input for a next iteration, i.e. in new_list instead of in old_list. And of course to initialize the variable before loop: new_list = old_list.
  2. Regex patterns should have r-prefix.
  3. As mentioned in comments, avoid naming variables with built-in names like dict and list.
JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement