Wednesday, June 17, 2026
HomeLanguagesPython – Actual order index distance

Python – Actual order index distance

Sometimes we have an unsorted list and we wish to find the actual position the elements could be when they would be sorted, i.e we wish to construct the list which could give the position distance to each element destined if the list was sorted. This has a good application in web development and competitive programming domain. Let’s discuss certain ways in which this can be done. 

Method #1 : Using sorted() + index() + list comprehension All the above function can combine to achieve this particular task. The sorted function returns the sorted order and the indexing is done by the index function. List comprehension does the task of doing for whole list elements and integrating both tasks. 

Python3




# Python3 code to demonstrate
# Actual order index distance
# using sorted() + index() + list comprehension
 
# initializing list
test_list = [6, 3, 1, 2, 5, 4]
 
# printing original list
print("The original list is : " + str(test_list))
 
# using sorted() + index() + list comprehension
# Actual order index distance
temp = sorted(test_list)
res = [abs(temp.index(ele) - idx) for idx, ele in enumerate(test_list)]
 
# printing result
print ("The relative ordering list is : " + str(res))


Output : 

The original list is : [6, 3, 1, 2, 5, 4]
The relative ordering list is : [5, 1, 2, 2, 0, 2]

Time complexity: O(nlogn) where n is the number of elements in the list “test_list”.

Auxiliary space: O(n) where n is the number of elements in the list “test_list”.

  Method #2 : Using map() + enumerate() + dictionary comprehension + sorted() The dictionary comprehension is used in place of list comprehension and the sorted list is formed and the index distance of actual ordering in sorted list are traversed using enumerate to have key-value pair and then are get by map for all the indices in list. 

Python3




# Python3 code to demonstrate
# Actual order index distance
# using map() + enumerate() + dictionary comprehension + sorted()
 
# initializing list
test_list = [6, 3, 1, 2, 5, 4]
 
# printing original list
print("The original list is : " + str(test_list))
 
# using map() + enumerate() + dictionary comprehension + sorted()
# Actual order index distance
temp = {val: abs(key - test_list.index(val)) for key, val in enumerate(sorted(test_list))}
res = list(map(temp.get, test_list))
 
# printing result
print ("The relative ordering list is : " + str(res))


Output : 

The original list is : [6, 3, 1, 2, 5, 4]
The relative ordering list is : [5, 1, 2, 2, 0, 2]

Time Complexity: O(n*nlogn), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n), where n is the number of elements in the list “test_list”.

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32516 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6898 POSTS0 COMMENTS
Nicole Veronica
12014 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6964 POSTS0 COMMENTS