 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Program to assign each list element value equal to its magnitude order
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.
Example
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)  Output
The list is : [91, 42, 27, 39, 24, 45, 53] The result is: [0, 2, 6, 1, 5, 3, 4]
Explanation
- 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. 
Advertisements
 