nice python solution

Magnus Lie Hetland mlh at
Mon Nov 19 00:43:47 CET 2001

"Uwe Schmitt" <uwe at> wrote in message
news:9t95gu$ab7l1$1 at
> Hi,
> i there a nice python shortcut for:
>     if idx=0:
>        a=val
>     elif idx=1:
>        b=val

This isn't really an answer to your question, perhaps, but have
you considered keeping the if-sentence? It's a very Pythonic and
readable thing to do, IMO. Trying to find fancy shortcuts will
often make your code less readable, although possibly more
fancy ;)

> ???
> I tried
>    (a,b)[idx] = val
> but this does not work, i get an error message.

Well... If you want an evil solution:

  locals()[['a','b'][idx]] = val


> Greetings, Uwe.
> --
> Dr. Uwe Schmitt                                 Uwe.Schmitt at

Magnus Lie Hetland                           The Anygui Project                 

More information about the Python-list mailing list