# Re: sequence multiplied by -1

Discussion in 'Python' started by Yingjie Lan, Sep 25, 2010.

1. ### Yingjie LanGuest

--- On Sat, 9/25/10, Thomas Jollans <> wrote:
> for every list l and integer n >= 0:
>     len(l*n) == len(l)*n

Well, this invariance is indeed broken under my proposal.
But it is *already broken* in current python3k.
However, the following invariance is maintained under
my proposal:

len(l*n) == len(l) * abs(n),

which is also broken under current python3k.
if you think len(..) as a mathematical norm,
the above invariance makes perfect sense:

|| a * b || == ||a|| * |b|, b is real

>
> > Simply put, a sequence multiplied by -1 can give a

> reversed sequence.
>
> For that, we have slicing. A negative step value produces a
> reverse slice of
> the list. You can't argue that this makes sense, can you
>
> >>> [1,2,3,4][::-1]

> [4, 3, 2, 1]
> >>> [1,2,3,4][::-2]

> [4, 2]
> >>>

Having more than one way of doing things sometimes is good.
Slicing is a little more complex, what if you want this:

>>> ([1,2,3,4]*2)[::-1]

[4, 3, 2, 1, 4, 3, 2, 1]

under my new proposal, you simply do this:

>>> [1,2,3,4]*-2

[4, 3, 2, 1, 4, 3, 2, 1]

Cheers,

Yingjie

Yingjie Lan, Sep 25, 2010

2. ### Stefan SchwarzerGuest

Hi,

On 2010-09-25 14:11, Yingjie Lan wrote:
> Having more than one way of doing things sometimes is good.

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.

One could argue that using L[::-1] isn't "obvious" but I
believe the rule refers to being obvious to people who have
used Python for a while. Besides that, multiplying a list
with a negative value to give a reverse list isn't so
intuitive either.

On 2010-09-25 13:45, Thomas Jollans wrote:
> Multiplying a list by a negative integer should produce a list of negative
> length, which does not exist. IMHO, the only correct behaviour would be to
> raise an exception, though one could argue that there are practical benefits
> for the operation to succeed for any integer operand.

I agree with raising an exception, probably a ValueError.

Stefan

Stefan Schwarzer, Sep 25, 2010

3. ### Dennis Lee BieberGuest

On Sat, 25 Sep 2010 06:54:36 -0700 (PDT), Yingjie Lan
<> declaimed the following in
gmane.comp.python.general:

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

>

At first glance, and if I didn't know that [] * # leads to
# copies of the contents of []... I'd expect that syntax to multiply
each element of "L" by a scalar -3...

Which is probably what one would get from a numpy array (or would
those require the form -3 * L ?)

This is already an area where Python's list manipulations conflict
with mathematical vector/scalar arithmetic; I'd not want to increase the
reversal operation too...
--
Wulfraed Dennis Lee Bieber AF6VN
HTTP://wlfraed.home.netcom.com/

Dennis Lee Bieber, Sep 25, 2010
4. ### Bruno DesthuilliersGuest

Stefan Schwarzer a écrit :

> One could argue that using L[::-1] isn't "obvious"

It *is* obvious - once you've learned slicing. "obvious" doesn't mean
you shouldn't bother reading the FineManual.

Bruno Desthuilliers, Oct 3, 2010