L = [1, 2, 3, 4, 6, 7, 8, 9, 12, 13, 19, 20, 22, 23, 40, 44]
G = (list(x) for _, x in groupby(L, lambda x, c=count(): next(c) - x))
print(','.join('-'.join(map(str, (g[0], g[-1])[:len(g)])) for g in G))
Pythonic way to convert a list of integers into a string of comma-separated ranges