Input = [("M", 19), ("H", 19), ("A", 25)]
Output =[("A", 25), ("M" ,19), ("H", 19)]
It should sort alphabetically but when the second value is equal then it should remain in place without changing their respective places. Here M and H both have value as 19 so it is already sorted.
You could group the items by the second value of each tuple using
itertools.groupby, sort groupings by the first item in each group, then flatten the result withitertools.chain.from_iterable:Output: