Skip to content
Advertisement

Find and replace string values in list [duplicate]

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

Advertisement

Answer

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

This is called a list comprehension.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement