# code to sort a list based on second index lst = [[1,"e"],[2,"t"],[9,"a"][5,"r"][4,"b"]] ans = sorted(lst,key=lambda x: (x[1], x[0]))