Find and replace string values in list

Eric Herlitz picture Eric Herlitz · Jun 29, 2010 · Viewed 410k times · Source

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']

Answer

sberry picture sberry · Jun 29, 2010
words = [w.replace('[br]', '<br />') for w in words]

These are called List Comprehensions.