Removing all occurences of a character from a string.

Sean Ross sross at chat.carleton.ca
Wed Sep 25 09:52:33 EDT 2002


Yes. That's exactly what I was looking for. Thank you very much.

Mike C. Fletcher (mcfletch at rogers.com) wrote:
> Under later Python versions (such as 2.2.1), you can do the following:

> attr = [[item.replace("'","") for item in element] for element in attr]

> Which is rather elegant IMO.  It also has the side-effect of _not_ 
> rewriting the data in-place in your original list, instead just 
> replacing that list when we are finished creating the new list, which 
> may or may not be desirable for you.

> HTH,
> Mike

> PS: Always nice to hear from another Canadian Pythonista :)

> Sean Ross wrote:
> > Thank you Justin.
> > 
> > How about this, then:
> > 
> > I have a list of lists of these strings and I want to apply replace to each
> > string in the list of lists.
> > 
> > So, say:
> > 
> > attr = [ ["'gill-attachment'", "'a'", "'d'", "'f'",
> > "'n'"],["'gill-spacing'", "'c'", "'d'", "'w'"], ["'gill-size'", "'b'",
> > "'n'"]]
> > 
> > I can do the following:
> > for i in xrange(0, len(attr)):
> >     for j in xrange(0,len(attr[i])):
> >          attr[i][j] = attr[i][j].replace("'","")
> > 
> > to get what I want; which is:
> > [ ['gill-attachment', 'a', 'd', 'f', 'n'],['gill-spacing', 'c', 'd', 'w'],
> > ['gill-size', 'b', 'n']]
> > 
> > Is there a nicer way to do this?
> > Sean
> ...





More information about the Python-list mailing list