Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the difference between two list?

Tags:

python

I have to lists of numbers, I want to save the difference between each parallel cells I need something like this:

diffEx = [(myEx - opEx) for myEx,opEx in (myExeptPack,opExeptPack)]

eg.

listA = [1,2,3]
listB = [4,3,2]
diff = [-3,-1,1]

Thank you

like image 413
ifryed Avatar asked Feb 09 '26 18:02

ifryed


2 Answers

Use zip function:

diffEx = [(myEx - opEx) for myEx,opEx in zip(myExeptPack,opExeptPack)]
like image 135
aga Avatar answered Feb 12 '26 06:02

aga


You could use map to make the statement more concise than when using zip:

import operator
diffEx = map(operator.sub, myExeptPack, opExeptPack)
like image 32
pstobiecki Avatar answered Feb 12 '26 08:02

pstobiecki



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!