Fix Python – Find and replace string values in list [duplicate]

Question

Asked By – Eric Herlitz

I got this list:

words = ['how', 'much', 'is[br]', 'the', 'fish[br]', 'no', 'really']

What I would like is to replace [br] with some fantastic value similar to <br /> and thus getting a new list:

words = ['how', 'much', 'is<br />', 'the', 'fish<br />', 'no', 'really']

Now we will see solution for issue: Find and replace string values in list [duplicate]


Answer

words = [w.replace('[br]', '<br />') for w in words]

These are called List Comprehensions.

This question is answered By – sberry

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0