'Create a sublist from a list. First with second, second with third, etc

From this

a = ['foo','bar','cat','dog']

to create another variable b that will have it as follows:

b = [['foo','bar'],['bar','cat'],['cat','dog']] # with this order

I have tried this:

b = [[i] * 2 for i in a]

but gives this:

[['foo', 'foo'], ['bar', 'bar'], ['cat', 'cat'], ['dog', 'dog']]

No external libraries please.



Solution 1:[1]

Try this:

a = ['foo','bar','cat','dog']
b = [x for x in zip(a, a[1:])]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 bbbbbbbbb