Q:

Join two offset lists ("offset zip"?)

Consider two lists, for example:
L = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
M = [1, 2, 3, 4, 5, 6, 7, 8]

Let's say we are given one pair of elements which must match - for example, (d, 6). Is it possible to "align" the lists at those elements, and then to join the lists where there are still elements in both lists - sort of like a cross between a zip and an inner join?
This is probably best illustrated by example. Using L and M as above:

(d, 6) would lead to [(a, 3), (b, 4), (c, 5), (d, 6), (e, 7), (f, 8)]
(h, 2) would lead to [(g, 1), (h, 2)]
(a, 8) would lead to [(a, 8)]

My context: I'm currently attempting to build a neural network capable of learning to play chess by reading chess notation. This question is to do with checking diagonals on the board in order to update piece positions. For example, if a white bishop has just moved to b7 (one square in from the lower-right corner of the board) then it must have come from a square on the h1-a8 long diagonal or from a square on the a6-c8 short diagonal.
So in my case, L and M are the same length, since they correspond to the ranks and files on an 8-by-8 chessboard. But in general I suppose the lists could be of different lengths.

A:

You could do something along the lines of
L = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
M = [1, 2, 3, 4, 5, 6, 7, 8]
m = L.index('h')
n = M.index(2)
if m > n:
  m, n = (m - n), 0
else:
  m, n = 0, (n - m)
print(list(zip(L[m:], M[n:])))

PS you could probably make the m, n index generation more compact but it is clearer what's supposed to happen with this structure.

