diff --git a/plotly/figure_factory/_scatterplot.py b/plotly/figure_factory/_scatterplot.py index 75895272615..271a35993dd 100644 --- a/plotly/figure_factory/_scatterplot.py +++ b/plotly/figure_factory/_scatterplot.py @@ -25,14 +25,15 @@ def endpts_to_intervals(endpts): """ length = len(endpts) # Check if endpts is a list or tuple - if not (isinstance(endpts, (tuple)) or isinstance(endpts, (list))): + if not isinstance(endpts, (list, tuple)): raise exceptions.PlotlyError( "The intervals_endpts argument must " "be a list or tuple of a sequence " "of increasing numbers." ) - # Check if endpts contains only numbers - for item in endpts: + # Single-pass validation for both string element and increasing check + prev = None + for i, item in enumerate(endpts): if isinstance(item, str): raise exceptions.PlotlyError( "The intervals_endpts argument " @@ -40,27 +41,20 @@ def endpts_to_intervals(endpts): "sequence of increasing " "numbers." ) - # Check if numbers in endpts are increasing - for k in range(length - 1): - if endpts[k] >= endpts[k + 1]: + if prev is not None and prev >= item: raise exceptions.PlotlyError( "The intervals_endpts argument " "must be a list or tuple of a " "sequence of increasing " "numbers." ) - else: - intervals = [] - # add -inf to intervals - intervals.append([float("-inf"), endpts[0]]) - for k in range(length - 1): - interval = [] - interval.append(endpts[k]) - interval.append(endpts[k + 1]) - intervals.append(interval) - # add +inf to intervals - intervals.append([endpts[length - 1], float("inf")]) - return intervals + prev = item + + # Build the intervals efficiently using list comprehensions + intervals = [[float("-inf"), endpts[0]]] + intervals.extend([[endpts[k], endpts[k + 1]] for k in range(length - 1)]) + intervals.append([endpts[-1], float("inf")]) + return intervals def hide_tick_labels_from_box_subplots(fig):