[Tutor] List element with replace

Alan Gauld alan.gauld@blueyonder.co.uk
Wed Apr 23 13:31:02 2003


> line 3   >>>  print mexT[0]
> line 4   >>>  ['74|', '51', '79|', '55', '79|']

Notice that mexT[0] is a list. Each element of the list is a string.

> line 5   >>>  mexT[0] = mexT[0].replace("|", "")
> AttributeError: 'list' object has no attribute 'replace'

You need to iterate over the list and do the replace on each 
element, like so:

mexT[0] = [ s.replace('|','') for s in mexT[0] ]

OR if you prefer map():

mexT[0] = map(lambda s: s.replace('|',''), mexT[0])

HTH,

Alan G.