1

I have the following rows in a pandas dataframe:

[11782,11785,11787,11787,11789,11792,11795]
[35206,35210,35212,35212,35214,35214,35216]

I want to create list of lists in this way:

[[11782,11785],[11787,11787],[11789,11792],[11795,...]
[[35206,35210],[35212,35212],[35214,35214],[35216,...]

How can I make this set of lists taking every two adjacent elements of a list?

Md Abrar Jahin
  • 374
  • 3
  • 9

1 Answers1

2

You could use a nested list comprehension:

out = [[li[i:i+2] for i in range(0, len(li), 2)] for li in s]

Output:

[[[11782, 11785], [11787, 11787], [11789, 11792], [11795]],
 [[35206, 35210], [35212, 35212], [35214, 35214], [35216]]]