187

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']
martineau
  • 112,593
  • 23
  • 157
  • 280
Eric Herlitz
  • 23,976
  • 26
  • 110
  • 154

5 Answers5

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

These are called List Comprehensions.

Teymour Aldridge
  • 1,546
  • 11
  • 25
sberry
  • 121,970
  • 18
  • 133
  • 163
  • 5
    Performing a comparison between this list comprehension method and the map method (posted by @Anthony Kong), this list method was roughly 2x faster. Also it allowed for inserting multiple replacements into the same call, e.g. `resname = [name.replace('DA', 'ADE').replace('DC', 'CYT').replace('DG', 'GUA').replace('DT', 'THY') for name in ncp.resname()]` – Steven C. Howell Apr 20 '15 at 18:50
  • 1
    @sberry I have a list `['word STRING', 'word_count BIGINT', 'corpus STRING', 'corpus_date BIGINT']` where I am trying to replace `'` with empty but this is not working. how can we replace this using this? – Sandeep Singh Jun 27 '18 at 10:56
  • What if one of the items is a float/integer? – Patriots299 Dec 31 '18 at 20:38
39

You can use, for example:

words = [word.replace('[br]','<br />') for word in words]
houbysoft
  • 31,018
  • 23
  • 95
  • 154
37

Beside list comprehension, you can try map

>>> map(lambda x: str.replace(x, "[br]", "<br/>"), words)
['how', 'much', 'is<br/>', 'the', 'fish<br/>', 'no', 'really']
Anthony Kong
  • 33,453
  • 37
  • 154
  • 277
19

In case you're wondering about the performance of the different approaches, here are some timings:

In [1]: words = [str(i) for i in range(10000)]

In [2]: %timeit replaced = [w.replace('1', '<1>') for w in words]
100 loops, best of 3: 2.98 ms per loop

In [3]: %timeit replaced = map(lambda x: str.replace(x, '1', '<1>'), words)
100 loops, best of 3: 5.09 ms per loop

In [4]: %timeit replaced = map(lambda x: x.replace('1', '<1>'), words)
100 loops, best of 3: 4.39 ms per loop

In [5]: import re

In [6]: r = re.compile('1')

In [7]: %timeit replaced = [r.sub('<1>', w) for w in words]
100 loops, best of 3: 6.15 ms per loop

as you can see for such simple patterns the accepted list comprehension is the fastest, but look at the following:

In [8]: %timeit replaced = [w.replace('1', '<1>').replace('324', '<324>').replace('567', '<567>') for w in words]
100 loops, best of 3: 8.25 ms per loop

In [9]: r = re.compile('(1|324|567)')

In [10]: %timeit replaced = [r.sub('<\1>', w) for w in words]
100 loops, best of 3: 7.87 ms per loop

This shows that for more complicated substitutions a pre-compiled reg-exp (as in 9-10) can be (much) faster. It really depends on your problem and the shortest part of the reg-exp.

Jörn Hees
  • 3,109
  • 18
  • 41
5

An example with for loop (I prefer List Comprehensions).

a, b = '[br]', '<br />'
for i, v in enumerate(words):
    if a in v:
        words[i] = v.replace(a, b)
print(words)
# ['how', 'much', 'is<br/>', 'the', 'fish<br/>', 'no', 'really']
Waket Zheng
  • 3,548
  • 1
  • 14
  • 26