Get the next element of list in Python

First format your list of string into a list of list, then do a mapping by zip.

i = [i.split() for i in lst]

f = [f"{x} {y}" for item in i for x,y in zip(item,item[1::])]

print (f)

#['A B', 'B C', 'C D', 'E F', 'F G', 'G H', 'H I', 'I J', 'K L', 'L M', 'M N']

Your problem is that you're flattening the whole list and dividing to couples when you want to divide to subsequent couples only the inner elements. So for that we will perform the operation on each element separatly:

lst = ['A B C D','E F G H I J','K L M N']

res = []
for s in lst:
    sub_l = s.split()
    for i in range(len(sub_l)-1):
        res.append("{} {}".format(sub_l[i], sub_l[i+1]))
print(res)

Gives:

['A B', 'B C', 'C D', 'E F', 'F G', 'G H', 'H I', 'I J', 'K L', 'L M', 'M N']

Tags:

Python

List