for _ in range(len(input)):
    new_list.append((elem, input_dict[elem]))
elem = input_dict[elem]
if elem not in input_dict:
    raise Exception('key {} not found in dict'.format(elem))
Sort a list of tuples in consecutive order