[Python-ideas] ('blue', 'red', 'orange' if something, 'green')

cool-RR cool-rr at cool-rr.com
Fri Apr 22 12:48:33 CEST 2011


On Fri, Apr 22, 2011 at 12:42 PM, Nick Coghlan <ncoghlan at gmail.com> wrote:

> On Fri, Apr 22, 2011 at 7:59 PM, cool-RR <cool-rr at cool-rr.com> wrote:
> > Here's an idea that would have helped me today while coding. Allow
> something
> > like this:
> >     ('blue', 'red', 'orange' if some_condition, 'green')
> > So 'orange' is included in the tuple only if `some_condition` evaluates
> to
> > `True`. This could be applied to list literals, tuple literals, set
> > literals, and possibly dict literals, though the latter might be too
> clunky.
> > I expect this to be rejected, but I really couldn't think of an elegant
> way
> > to achieve the same thing with existing syntax.
>
> colours = 'blue red orange green'.split()
> if not some_conditions:
>    colours.remove('orange')
>
> There are lots of options, but most of them start by not using a tuple
> for a variable length sequence of like items.
>
> Cheers,
> Nick.


I see. It's about as elegant as the other suggestions. And it's pretty
annoying to use a list when I really wanted to use a tuple. Yeah, I can
convert it to a tuple at the end, but that's just making it more verbose.


Ram.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20110422/2d61abcd/attachment.html>


More information about the Python-ideas mailing list