Python | Sort tuple list on basis of difference of elements

Sometimes, while working with data, we can have a problem of sorting them. There are many types of basis on which sorting can be performed. But this article discusses sorting on basis of difference of both elements of pair. Let’s discuss certain ways in which this can be done.

Method #1 : Using sorted() + lambda
The combination of above functionalities can be used to solve this problem. In this, sorting is performed by sorted() and lambda function is fed as key to perform desired sorting.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Sort tuple list on basis of difference of elements
# using sorted() + lambda
  
# initialize list 
test_list = [(1, 4), (6, 5), (8, 10)]
  
# printing original list 
print("The original list : " + str(test_list))
  
# Sort tuple list on basis of difference of elements
# using sorted() + lambda
res = sorted(test_list, key = lambda sub: abs(sub[1] - sub[0]))
  
# printing result
print("List after sorting by difference : " + str(res))

chevron_right


Output :



The original list : [(1, 4), (6, 5), (8, 10)]
List after sorting by difference : [(6, 5), (8, 10), (1, 4)]

 

Method #2 : Using sort() + comparator + cmp()
This is yet another way to perform this task. In this, we pass the sort(), a comparator function which performs the similar difference logic using cmp(). Works only in Python2.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to demonstrate working of
# Sort tuple list on basis of difference of elements
# using sort() + comparator + cmp()
  
# comparator function
def diff_sort(ele1, ele2):
     return cmp(ele2[0] - ele2[1], ele1[0] - ele1[1])
  
# initialize list 
test_list = [(1, 4), (6, 5), (8, 10)]
  
# printing original list 
print("The original list : " + str(test_list))
  
# Sort tuple list on basis of difference of elements
# using sort() + comparator + cmp()
test_list.sort(diff_sort)
  
# printing result
print("List after sorting by difference : " + str(test_list))

chevron_right


Output :

The original list : [(1, 4), (6, 5), (8, 10)]
List after sorting by difference : [(6, 5), (8, 10), (1, 4)]


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.