Velocity Reviews > How to efficiently proceed addition and subtraction in python list?

# How to efficiently proceed addition and subtraction in python list?

Daniel Mark
Guest
Posts: n/a

 09-18-2006
Hello all:

I have a list AAA = [1, 2, 3] and would like to subtract one from list
AAA
so AAA' = [0, 1, 2]

What should I do?

Thank you
-Daniel

Tim Chase
Guest
Posts: n/a

 09-18-2006
> I have a list AAA = [1, 2, 3] and would like to subtract one from list
> AAA
> so AAA' = [0, 1, 2]
>
> What should I do?

Sounds like a list comprehension to me:

>>> a = [1,2,3]
>>> a_prime = [x-1 for x in a]
>>> a_prime

[0, 1, 2]

-tkc

Chiucs
Guest
Posts: n/a

 09-19-2006
try this
a=[1, 2, 3]
b=a[:]

"Daniel Mark" <(E-Mail Removed)> ???
news:(E-Mail Removed) oups.com ???...
> Hello all:
>
> I have a list AAA = [1, 2, 3] and would like to subtract one from list
> AAA
> so AAA' = [0, 1, 2]
>
> What should I do?
>
>
> Thank you
> -Daniel
>

Paul Rubin
Guest
Posts: n/a

 09-19-2006
"Daniel Mark" <(E-Mail Removed)> writes:
> I have a list AAA = [1, 2, 3] and would like to subtract one from list
> AAA
> so AAA' = [0, 1, 2]
>
> What should I do?

BBB = [x-1 for x in AAA]

Ant
Guest
Posts: n/a

 09-19-2006

Tim Chase wrote:
> > I have a list AAA = [1, 2, 3] and would like to subtract one from list
> > AAA
> > so AAA' = [0, 1, 2]
> >
> > What should I do?

>
>
> Sounds like a list comprehension to me:

Also the built in function 'map' would work:

>>> a = [1,2,3]
>>> b = map(lambda x: x-1, a)
>>> b

[0, 1, 2]

List comprehensions are more pythonic, but map would probably be faster
if performance was a (real) issue.

Steve Holden
Guest
Posts: n/a

 09-19-2006
Ant wrote:
> Tim Chase wrote:
>
>>>I have a list AAA = [1, 2, 3] and would like to subtract one from list
>>>AAA
>>>so AAA' = [0, 1, 2]
>>>
>>>What should I do?

>>
>>
>>Sounds like a list comprehension to me:

>
>
> Also the built in function 'map' would work:
>
>
>>>>a = [1,2,3]
>>>>b = map(lambda x: x-1, a)
>>>>b

>
> [0, 1, 2]
>
> List comprehensions are more pythonic, but map would probably be faster
> if performance was a (real) issue.
>

And statements like that are probably going to annoy me

>>> t = timeit.Timer("b = map(lambda x: x-1, a)", setup="a=[1,2,3]")
>>> t.timeit()

2.4686168214116599
>>> t = timeit.Timer("b = [x-1 for x in a]", setup="a=[1,2,3]")
>>> t.timeit()

0.9930245324475635
>>>

Any timing prediction involving the word "probably" isn't worth the
paper it's written on (or even less if it's posted in a newsgroup .
If it's "probably" faster, and if performance is *really* important, you
need to benchmark both options to remove the "probably". As the above
test makes clear, your assertions are certainly untrue for 2.4.2 on Windows.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

Fredrik Lundh
Guest
Posts: n/a

 09-19-2006
Steve Holden wrote:

> And statements like that are probably going to annoy me
>
> >>> t = timeit.Timer("b = map(lambda x: x-1, a)", setup="a=[1,2,3]")
> >>> t.timeit()

> 2.4686168214116599
> >>> t = timeit.Timer("b = [x-1 for x in a]", setup="a=[1,2,3]")
> >>> t.timeit()

> 0.9930245324475635
> >>>

And now someone's probably reading this and thinking that list comprehensions
are *always* faster than "map"...

</F>

Steve Holden
Guest
Posts: n/a

 09-19-2006
Fredrik Lundh wrote:
> Steve Holden wrote:
>
>
>>And statements like that are probably going to annoy me
>>
>> >>> t = timeit.Timer("b = map(lambda x: x-1, a)", setup="a=[1,2,3]")
>> >>> t.timeit()

>>2.4686168214116599
>> >>> t = timeit.Timer("b = [x-1 for x in a]", setup="a=[1,2,3]")
>> >>> t.timeit()

>>0.9930245324475635
>> >>>

>
>
> And now someone's probably reading this and thinking that list comprehensions
> are *always* faster than "map"...
>

You are so right. So, let me repeat the important part that Fredrik
didn't quote:

"""if performance is *really* important, you
need to benchmark both options"""

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

Ant
Guest
Posts: n/a

 09-19-2006

Steve Holden wrote:
> Fredrik Lundh wrote:
> > Steve Holden wrote:
> >

....
> """if performance is *really* important, you
> need to benchmark both options"""

Fair point.

Simon Brunning
Guest
Posts: n/a

 09-19-2006
On 18 Sep 2006 15:43:31 -0700, Daniel Mark <(E-Mail Removed)> wrote:
> Hello all:
>
> I have a list AAA = [1, 2, 3] and would like to subtract one from list
> AAA
> so AAA' = [0, 1, 2]

assuming that by "efficient" you mean in terms of CPU. If, instead,
you are concerned with memory usage, you might instead do something
like:

for index, value in enumerate(AAA):
AAA[index] = value-1

--
Cheers,
Simon B,
http://www.velocityreviews.com/forums/(E-Mail Removed)