Removing all occurences of a character from a string.

Cameron Laird claird at starbase.neosoft.com
Wed Sep 25 11:15:36 EDT 2002


In article <mailman.1032928353.13514.python-list at python.org>,
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]
			.
			.
			.
I growsed at Alex privately that someone ought to mention
map.  My complaint was ill-considered; this problem *does*
present an excellent example of why list comprehensions are
absolutely more readable and therefore Pythonic than the
corresponding map()- (or filter()-) coded solution.  Unless
the atomic operation (the replace, in this case) happens to
be naturally expressible without passing along more arguments,
map() complexifies excessively with lambdas or equivalents.
In this case, of course, there needs to be a replace("'", ""),
so list comprehension is a clear win.

I still feel obliged to observe that one's likely to come
across old code that solves this sort of problem through the
use of map and lambda.  Maybe I'm also making a mistake in
that.  'Anyone have a good sense, for instance, of what
Python version the leading Linux distributions bundle?
-- 

Cameron Laird <Cameron at Lairds.com>
Business:  http://www.Phaseit.net
Personal:  http://phaseit.net/claird/home.html



More information about the Python-list mailing list