Question: On python, I have a function called get_list_axes which takes 2 parameters :  A list of unique captors - let's call them ["A", "B", "C", D"]. A list of captors from the unique captors above which should share the same y-axis - For instance ["A", "D"] or ["A", "C"] or ["A", "B", "D"], etc.    The goal of the function is to return a list of y-axis depending on the list of captors to merge on the same y-axis. When there is nothing to merge, it should then return ["y1", "y2", "y3", "y4"].  The function is as follows : def get_list_axes(list_ana_captors: list, merge_captors: list) -> list: counter_no_merge, counter_merge = 1, 1 list_axes = ["y1"] for captor in list_ana_captors[1:]: if captor not in merge_captors or captor == merge_captors[0]: if captor == merge_captors[0]: counter_merge = counter_no_merge + 1 counter_no_merge = counter_no_merge + 1 if captor not in merge_captors: list_axes.append(f"y{counter_no_merge}") else: list_axes.append(f"y{counter_merge}") return list_axes   Now, let's define our list of unique captors list_captors = ["A", "B", "C", "D"]. Basically, my problem is that whenever the list of captors to merge is in the same order than the list of captors list_captors, everything works, for instance :  get_list_axes(list_captors, ["A", "B", "C", "D"]) = ['y1', 'y1', 'y1', 'y1'] get_list_axes(list_captors, ["B", "C", "D"]) = ['y1', 'y2', 'y2', 'y2'] get_list_axes(list_captors, ["A", "B"]) = ['y1', 'y1', 'y2', 'y3'] get_list_axes(list_captors, ["A", "C"]) = ['y1', 'y2', 'y1', 'y3'] get_list_axes(list_captors, ["A", "D"]) = ['y1', 'y2', 'y3', 'y1'] get_list_axes(list_captors, ["B", "D"]) = ['y1', 'y2', 'y3', 'y2']  However, if we try in reverse :  get_list_axes(list_captors, ["D", "A"]) = ['y1', 'y2', 'y3', 'y4'] instead of ['y1', 'y2', 'y3', 'y1'] get_list_axes(list_captors, ["D", "C"]) = ['y1', 'y2', 'y1', 'y3'] instead of ['y1', 'y2', 'y3', 'y3'] get_list_axes(list_captors, ["D", "B"]) = ['y1', 'y1', 'y2', 'y3'] instead of ['y1', 'y2', 'y3', 'y2'] get_list_axes(list_captors, ["D", "C", "A", "B"]) = ['y1', 'y1', 'y1', 'y2'] instead of ['y1', 'y1', 'y1', 'y1']  Is there an easier way than what I am currently doing ?  Session Info : ----- session_info 1.0.0 ----- IPython 8.12.3 jupyter_client 8.6.2 jupyter_core 5.7.2 ----- Python 3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:57:54) [MSC v.1924 64 bit (AMD64)] Windows-10-10.0.19041-SP0 ----- Session information updated at 2024-07-30 15:15 

Answer: Your original get_list_axes function fails when merge_captors is not in the same order as list_ana_captors. This is because it relies on the specific order of captors, causing incorrect y-axis assignments. To fix this, we map each captor to its y-axis, ensuring captors in merge_captors share the same y-axis regardless of their order. Code: def get_list_axes(list_ana_captors: list, merge_captors: list) -> list: list_axes = [] captor_to_axis = {} current_y = 1 for captor in list_ana_captors: if captor in merge_captors: if merge_captors[0] not in captor_to_axis: captor_to_axis[merge_captors[0]] = f"y{current_y}" current_y += 1 captor_to_axis[captor] = captor_to_axis[merge_captors[0]] else: captor_to_axis[captor] = f"y{current_y}" current_y += 1 for captor in list_ana_captors: list_axes.append(captor_to_axis[captor]) return list_axes  Hope it will help, if you have any question feel free to ask.