adding backslash to symbols in python while using the regex library
Question: So I need to selectively add backslashes to characters that already have pre-
defined meaning (such as + and *)in the re library in python.

Say that I am give an array

    
    
    arr = ["five", "+", "two", "*", "zero", "=", "five"]
    

are there any functions within re (or python ) that will allow me to add a \
to "+" and " * ", so it will look like

    
    
    arr = ["five", "\+", "two", "\*", "zero", "=", "five"] ?
    

I can technically do exhausive search and coompare each element within arr to
one of the symbols and add \ accordingly, but that is far from ideal. Any help
would be appreciated!


Answer: Use [`re.escape`](https://docs.python.org/2/library/re.html#re.escape) with
either [`map`](https://docs.python.org/2/library/functions.html#map) or a list
comprehension:

    
    
    >>> import re
    >>> arr = ["five", "+", "two", "*", "zero", "=", "five"]
    >>> map(re.escape, arr)
    ['five', '\\+', 'two', '\\*', 'zero', '\\=', 'five']
    >>> [re.escape(x) for x in arr]
    ['five', '\\+', 'two', '\\*', 'zero', '\\=', 'five']
    

