When it is required to assign each list element value equal to its magnitude order, the ‘set’ operation, the ‘zip’ method and a list comprehension are used.
Below is a demonstration of the same
my_list = [91, 42, 27, 39, 24, 45, 53] print("The list is : ") print(my_list) my_ordered_dict = dict(zip(list(set(my_list)), range(len(set(my_list))))) my_result = [my_ordered_dict[elem] for elem in my_list] print("The result is: ") print(my_result)
The list is : [91, 42, 27, 39, 24, 45, 53] The result is: [0, 2, 6, 1, 5, 3, 4]
A list is defined and is displayed on the console.
The unique elements of the list are obtained, and it is converted into a list, and zipped.
It is then converted into a dictionary.
This is assigned to a variable.
This is the output that is displayed on the console.