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

Discussion in 'Python' started by Daniel Mark, Sep 18, 2006.

1. ### Daniel MarkGuest

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

Daniel Mark, Sep 18, 2006

2. ### Tim ChaseGuest

Re: How to efficiently proceed addition and subtraction in pythonlist?

> 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

Tim Chase, Sep 18, 2006

3. ### ChiucsGuest

try this
a=[1, 2, 3]
b=a[:]

"Daniel Mark" <> ???
news: ???...
> 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
>

Chiucs, Sep 19, 2006
4. ### Paul RubinGuest

"Daniel Mark" <> 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]

Paul Rubin, Sep 19, 2006
5. ### AntGuest

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.

Ant, Sep 19, 2006
6. ### Steve HoldenGuest

Re: How to efficiently proceed addition and subtraction in pythonlist?

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

Steve Holden, Sep 19, 2006
7. ### Fredrik LundhGuest

Re: How to efficiently proceed addition and subtraction in pythonlist?

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>

Fredrik Lundh, Sep 19, 2006
8. ### Steve HoldenGuest

Re: How to efficiently proceed addition and subtraction in pythonlist?

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

Steve Holden, Sep 19, 2006
9. ### AntGuest

Re: How to efficiently proceed addition and subtraction in pythonlist?

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

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

Fair point.

Ant, Sep 19, 2006
10. ### Simon BrunningGuest

Re: How to efficiently proceed addition and subtraction in pythonlist?

On 18 Sep 2006 15:43:31 -0700, Daniel Mark <> 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,

Simon Brunning, Sep 19, 2006