You can try the following: Determine the difference for each pair of consecutive numbers, and from those that determine the average difference.
nums = [101, 107, 106, 199, 204, 205, 207, 306, 310, 312, 312, 314, 317, 318, 380, 377, 379, 382, 466, 469, 471, 472, 557, 559, 562, 566, 569] pairs = list(zip(nums, nums[1:])) diffs = [abs(xy) for x, y in pairs] avg_diff = sum(diffs) / len(diffs)
Now you can group the numbers by whether the difference with the previous number is lower or higher than average:
groups = [[nums[0]]] # first group already has first number for (x, y), d in zip(pairs, diffs): if d < avg_diff: groups[-1].append(y) # add to last group else: groups.append([y]) # start new group
Or, if you prefer single-line lines spanning three lines, this might be for you:
groups = [functools.reduce(lambda A, b: A+(b[1],) if A else b, group, None) for key, group in itertools.groupby(zip(nums, nums[1:]), key=lambda t: abs(t[0]-t[1]) < 18.3) if key]
The result for your example is this:
[[101, 107, 106], [199, 204, 205, 207], [306, 310, 312, 312, 314, 317, 318], [380, 377, 379, 382], [466, 469, 471, 472], [557, 559, 562, 566, 569]]
Of course, this breaks down if there are groups with completely different group differences, as in [1, 4, 2, 5, 1042, 1230, 920, 3, 2, 5] . If so, you can try instead the relative difference of the numbers, for example. max(x,y)/min(x,y) instead of abs(xy) .