else in list comp

Cliff Wells clifford.wells at attbi.com
Wed Jan 29 07:17:04 CET 2003

On Tue, 2003-01-28 at 21:36, David Eppstein wrote:
> In article <mailman.1043813212.6357.python-list at python.org>,
>  Cliff Wells <clifford.wells at attbi.com> wrote:
> > Given the list:
> > 
> > l = ['mary', 'had', 'a', 'little', None]
> > 
> > it should get mapped to ['mary', 'had', 'a', 'little', '']
> [x or '' for x in l]

Yes, this was suggested by someone else, but again, solving the
particular problem wasn't the real point.  If the data contains anything
that evaluates to false (such as 0), then x or '' won't work.  I stated
the problem badly which led to this confusion.  My apologies.

Kindly reconsider the problem using 

myList = [0, None, '1', None, 2, None, '3']

My real interest was in a possible "else" clause (as per the subject
line) in a list comp.

['' for i in myList if i is None else i]

It seems a good idea to me, but then, I'm somewhat biased toward my own

> btw, l is a bad letter to use for a variable name (easy to confuse with 
> 1 or I or even |).


Cliff Wells <clifford.wells at attbi.com>

More information about the Python-list mailing list