[Python-ideas] ('blue', 'red', 'orange' if something, 'green')
Nick Coghlan
ncoghlan at gmail.com
Fri Apr 22 12:42:39 CEST 2011
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.
--
Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
More information about the Python-ideas
mailing list