default value in list comprehension

AlienBaby matt.j.warren at gmail.com
Mon Apr 19 14:26:42 CEST 2010


On Apr 19, 1:23 pm, eb303 <eric.brunel.pragma... at gmail.com> wrote:
> On Apr 19, 2:20 pm, AlienBaby <matt.j.war... at gmail.com> wrote:
>
>
>
>
>
> > Hi,
>
> > just a quick one,
>
> > Is it possible to achieve a default value in a list comprehension
> > where the if-clause is false?
>
> > Ie, something similar to:
>
> > [ a for a in b if something(a) else 'default' ]
>
> > the idea being that, rather than skip a value if the if-clause is
> > false, to place a default value at that position in the returned list
> > instead.
>
> > ?
>
> > Thanks,
>
> > Matt.
>
> [a if something(a) else 'default' for a in b]
>
> HTH
>  - Eric -- Hide quoted text -
>
> - Show quoted text -

Ahh.  Gotcha, thankyou :)



More information about the Python-list mailing list