4

I have a list that looks like this:

lst = [4, 6, 7, 8, 12, 15, 16, 17, 18, 19, 9, 28, 29]

I'd like to generate a nested list where the original list is broken into sublist of consecutive numbers:

sub_list = [[4], [6, 7, 8, 9], [12], [15, 16, 17, 18, 19], [28, 29]

I'm struggling to figure out how I can do this. Can anyone provide a function to generate this?

turtle
  • 7,533
  • 18
  • 68
  • 97

0 Answers0