# Convert Numpy Array To A Range

## 18 February 2022 - 1 answer

I have a sorted list of numbers:

``````[ 1,  2,  3,  4,  6,  7,  8,  9, 14, 15, 16, 17, 18, 25,
26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 38, 45]
``````

I want to know if numpy has an inbuit feature to get something like this out of it:

``````[ [1, 4], [6, 9], [14, 18], [25,36], [38], [45] ]
``````

Also great if it can ignore holes of certain 2-3 numbers missing in between would still make the range.

I am basically listing frame numbers of a video for processing -- so rather than list out all the frame numbers I would jut put in ranges and its okay if a 3-4 frames in between are missing.

Just want to know if there something that already implements this logic as it seems like a common thing people would want to do - otherwise, I'll implement it myself.

Edit: found a very close question that's answered already: converting a list of integers into range in python

There is no native numpy function but you could use `diff` + `where` + `split` + list comprehension:
``````>>> [[ary[0], ary[-1]] if len(ary)>1 else [ary[0]] for ary in np.split(arr, np.where(np.diff(arr, prepend=0)!=1)[0])]