# Python function to get numbers around another number

I made a function to get a list with numbers around another number. Example: I want to get in a range of 2 numbers around 4. This will return: [2, 6]. You can set a min number and a max_number. That means when I set a min_num, the lower number should not be lower than the min number.

import sys

def get_numbers_around(number: int, min_num: int = None, max_num: int = None, _range: int = 2, on_fail_no_error: bool = False) -> range:
if max_num is None:
max_num = sys.maxsize

if min_num is None:
min_num = -sys.maxsize - 1

diff = max_num - min_num
if diff < _range:
if not on_fail_no_error:
raise ValueError(
"Range is too small. Range is {0} big but difference between max_num and min_num is {1} big! ({0} < {1}".format(
range, max_num - min_num))
else:
return range(number - diff, number + diff)

final_list = [number - _range, number + _range]

if final_list[0] < min_num:
diff = abs(min_num - final_list[0])
final_list[0] = min_num
final_list[1] += diff

if final_list[1] > max_num:
diff = abs(final_list[1] - max_num)
final_list[1] = max_num
final_list[0] -= diff

return range(final_list[0], final_list[1])

print(get_numbers_around(4)) # range(2, 6)
print(get_numbers_around(4, _range=5)) # range(-1, 9)
print(get_numbers_around(0, min_num=0, max_num=1, _range=4, on_fail_no_error=True)) # range(-1, 1)
print(get_numbers_around(0, min_num=0, max_num=1, _range=4)) # ValueError


I think it is possible to optimize this code but I don't know how. Does anoyne have any idea?

• Your code seems really complicated, and so you should follow KISS and YAGNI.
• max_num and min_num don't work the way I would expect them to.
• The error goes against what I think is Pythonic. Just return the smaller set.
def get_numbers_around(number, size):
return range(number - size, number + size)


If you then need to implement min_num and max_num create a filter:

def filter_between(values, minimum=float('-inf'), maximum=float('inf')):
return (v for v in values if minimum <= v <= maximum)


>>> get_numbers_around(4, 5)
range(-1, 9)
>>> list(filter_between(_, 0))
[0, 1, 2, 3, 4, 5, 6, 7, 8]

• This interface is much more pythonic. But there is some utility in returning a range object; in particular, iterating through a generator should be faster without checking that each values is in range. Why not just return range(max(values.start, minimum), min(values.stop, maximum)). Commented May 27, 2019 at 20:07
• @BenjaminKuykendall I don't know how the code is being used. The person seems to like over-complicating things, and needs YAGNI and KISS in their life. Yes you could do it that way, but I still think YAGNI. Commented May 27, 2019 at 20:11
• But with this method there will be missing some numbers. Example: I have the number 1 and I want 2 numbers around. Then I'll get [-1, 0, 1, 2, 3] (I have 5 numbers now) . When I filter this, I'll get [0, 1, 2] (Now I only have 3 numbers but this should be 5.). The list should be like this: [0, 1, 2, 3, 4] . Commented May 31, 2019 at 5:00
• @Myzel394 Yours doesn't do this either. You can even see it in your examples above. range doesn't include the last value, like you think it does. Commented May 31, 2019 at 8:01