sequence multiplied by -1

Yingjie Lan lanyjie at
Sat Sep 25 15:54:36 CEST 2010


> In my opinion this _isn't_ a situation where it's good. :)
>     L[::-1]
> is only marginally longer than
>     -1 * L
> I think this small gain doesn't justify "violating" this
> "Python Zen" rule (from `import this`):
>     There should be one-- and preferably only one
> --obvious way to do it.

Thanks for the insightful remarks. For the rule above,
how about the case to reverse and multiply:

>>> L*-3 #L reversed and repeated three times


>>> L[::-1]*3 #L reversed and repeated three times

The first one is simpler (4 chars v.s. 9 chars).
I thought it was also intuitive because if you multiply
a vector by -1, you should get a vector 
in the reversed direction. But, intuitiveness depends
on who you are, what you do, etc....




More information about the Python-list mailing list