Python | Ways to sort a zipped list by values

Zipped lists are those lists where several lists are mapped together to form one list which can be used as one entity altogether. In Python Zip() function is used to map different lists.

Let’s discuss a few methods to demonstrate the problem.

Method #1: Using lambda and sort

filter_none

edit
close

play_arrow

link
brightness_4
code

# Pyhton code to demonstrate
# sort zipped list by values
# using lambda and sorted
  
# Declaring initial lists
list1 = ['geeks', 'for', 'Geeks']
list2 = [3, 2, 1]
zipped = zip(list1, list2)
  
# Converting to list
zipped = list(zipped)
  
# Printing zipped list
print("Initial zipped list - ", str(zipped))
  
# Using sorted and lambda
res = sorted(zipped, key = lambda x: x[1])
      
# printing result
print("final list - ", str(res))

chevron_right


Output:

Initial zipped list -  [('geeks', 3), ('for', 2), ('Geeks', 1)]
final list -  [('Geeks', 1), ('for', 2), ('geeks', 3)]

 
Method #2: Using operator and sort

filter_none

edit
close

play_arrow

link
brightness_4
code

# Pyhton code to demonstrate
# sort zipped list by values
# using operator and sorted
  
import operator
# Declaring initial lists
list1 = ['akshat', 'Manjeet', 'nikhil']
list2 = [3, 2, 1]
zipped = zip(list1, list2)
  
# Converting to list
zipped = list(zipped)
  
# Printing zipped list
print("Initial zipped list - ", str(zipped))
  
# Using sorted and operator
res = sorted(zipped, key = operator.itemgetter(1))
      
# printing result
print("final list - ", str(res))

chevron_right


Output:

Initial zipped list -  [('akshat', 3), ('Manjeet', 2), ('nikhil', 1)]
final list -  [('nikhil', 1), ('Manjeet', 2), ('akshat', 3)]


My Personal Notes arrow_drop_up

Programming freaktech Enthusiast and have interest in learning new upcoming technologies

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.