- Trending Categories
- Data Structure
- Operating System
- C Programming
- 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.
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.
- Assign ids to each unique value in a Python list
- Python – Assign Alphabet to each element
- Assign value to unique number in list in Python
- Python Program to find the cube of each list element
- Program to find an element in list whose value is same as its frequency in Python
- Program to perform given operation with each element of a list and given value in Python
- Python program to Sort a List of Tuples in Increasing Order by the Last Element in Each Tuple
- Python program to get the indices of each element of one list in another list
- How to add a new value to each element of list in R?
- Python program to Order Tuples using external List
- Sort a List of Tuples in Increasing Order by the Last Element in Each Tuple using Python program
- Program to find replicated list by replicating each element n times
- Program to check whether each node value except leaves is sum of its children value or not in Python
- Assign range of elements to List in Python
- Update each element in tuple list in Python