I try to add "G:" in the beginning and a backslash before every point of each element in a list. Therefore I created this example list1:
list1 = ['AEX.EN', 'AXAL.OQ', 'AAPIOE.NW']
And I need something like list2:
list2 = ['G:AEX\.EN', 'G:AXAL\.OQ', 'G:AAPIOE\.NW']
Thank you very much for the help!
CodePudding user response:
Use:
>>> ['G:' i.replace('.', '\\.') for i in list1]
['G:AEX\\.EN', 'G:AXAL\\.OQ', 'G:AAPIOE\\.NW']
>>>
In this case I prefer re.escape
:
>>> import re
>>> ['G:' re.escape(i) for i in list1]
['G:AEX\\.EN', 'G:AXAL\\.OQ', 'G:AAPIOE\\.NW']
>>>
CodePudding user response:
You can use
for join string then use replace()
like below:
>>> list1 = ['AEX.EN', 'AXAL.OQ', 'AAPIOE.NW']
>>> [('G:' l).replace('.','\.') for l in list1]
['G:AEX\\.EN', 'G:AXAL\\.OQ', 'G:AAPIOE\\.NW']