How to efficiently proceed addition and subtraction in python list?

D

Daniel Mark

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
 
P

Paul Rubin

Daniel Mark said:
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]
 
S

Steve Holden

Ant said:
Tim said:
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
 
F

Fredrik Lundh

Steve said:
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>
 
S

Steve Holden

Fredrik said:
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
 
S

Simon Brunning

Hello all:

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

You've had some excellent suggestions as to how to go about this
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top