This solution slows down significantly as the lists get large / many, but do not assume that the input lists are preconfigured:
from itertools import product def smallest_range(*arrays): result = min((sorted(numbers) for numbers in product(*arrays)), key=lambda n: abs(n[0] - n[-1])) return (result[0], result[-1]) list1 = [228, 240, 254, 301, 391] list2 = [212, 345, 395] list3 = [15, 84, 93, 103, 216, 398, 407, 488] print(smallest_range(list1, list2, list3))
APPLICATION:
list1 = [228, 240, 254, 301, 391] list2 = [212, 345, 395] list3 = [15, 84, 93, 103, 216, 398, 407, 488] print(smallest_range(list1, list2, list3))
PRINCE:
(391, 398)
source share