There are many ways to take the difference of neighboring elements and create a new difference list. Examples are given here. However, I have too many lists and I need to create difference lists of each. I do not want to write the same thing over and over. What is the shortest (and/or) efficient way to do it?
For example, say, I have list1, list2, ..., listn and I want to create d_list1, d_list2, ..., d_listn. Where d_list is the list that is created by list1[i+1]-list1[i]. Same applies for d_list2 where elements are list2[i+1]-list2[i].
Put all your lists in a list of lists. Then zip them together with an offset. This will give you pairs of lists. Then zip those and subtract:
This will give you a list of lists of differences:
If you truly have a lot of lists, it may be worth using Numpy, which has a
difffunction:Giving:
EDIT based on comment
To take the diff on the other axis, you don't need the outside zip, just the inner one:
Giving:
And in Numpy, just change the axis:
which produces the same as a Numpy array: