else in list comp
Andrew Bennetts
andrew-pythonlist at puzzling.org
Tue Jan 28 23:26:51 EST 2003
On Tue, Jan 28, 2003 at 08:05:25PM -0800, Cliff Wells wrote:
> Maybe this has come up before, but I haven't heard a discussion of list
> comps in a while so here's something I thought might be interesting:
>
> I was looking for a fast, clean way of converting a single value in a
> list to a different value (specifically None to ''). It occurred to me
> to use a list comp and this seemed the most straightforward solution:
>
> Given the list:
>
> l = ['mary', 'had', 'a', 'little', None]
>
> it should get mapped to ['mary', 'had', 'a', 'little', '']
This doesn't solve the general problem, but...
>>> l = ['mary', 'had', 'a', 'little', None]
>>> [x or '' for x in l]
['mary', 'had', 'a', 'little', '']
-Andrew.
More information about the Python-list
mailing list