I'm working with a dataset in Python where I have a list of tuples, each representing an interval (start, end). I've encountered a scenario where some of these intervals overlap, and I need to merge these overlapping intervals into a single interval that covers the entire range of the overlapping intervals. The goal is to reduce the list to only include non-overlapping intervals.
For example, given the list [(1, 3), (2, 4), (5, 7), (6, 8)]
The desired output is [(1, 4), (5, 8)].
Here's what I've tried so far:
def merge_intervals(intervals):
sorted_intervals = sorted(intervals, key=lambda x: x[0])
merged = []
for interval in sorted_intervals:
if not merged or merged[-1][1] < interval[0]:
merged.append(interval)
else:
merged[-1] = (merged[-1][0], max(merged[-1][1], interval[1]))
return merged
my_intervals = [(1, 3), (2, 4), (5, 7), (6, 8)]
print(merge_intervals(my_intervals))
This solution seems to work, but I'm concerned about its efficiency, especially for very large lists of intervals. I'm looking for any advice on optimizing this algorithm for better performance or if there's a more "Pythonic" way to approach this problem. Additionally, I'm curious if there are built-in functions or libraries in Python that could simplify this task.

