Python | Ways to sort a zipped list by values
Last Updated :
26 Dec, 2024
Sorting a zipped list in Python refers to arranging pairs of elements, typically tuples, based on their values. This allows us to reorder the list according to the second item in each pair, while keeping the key-value relationship intact. In this article we'll explore ways to sort a zipped list by values.
Using sorted() with operator.itemgetter()
operator.itemgetter()
is a fast way to sort a zipped list by values. It efficiently retrieves specific elements from tuples, making it ideal for sorting large datasets.
Example:
Python
import operator
a = ['akshat', 'Manjeet', 'nikhil']
b = [3, 2, 1]
zipped = zip(a,b)
# Converting to list
zipped = list(zipped)
# Using sorted and operator
res = sorted(zipped, key = operator.itemgetter(1))
print(str(res))
Output[('akshat', 3), ('Manjeet', 2), ('nikhil', 1)]
Explanation:
- This code zips lists
a
and b
into pairs. - It sorts the pairs by the second element .
- Result is a list sorted by values in
b
in ascending order.
Let's understand more ways to sort a zipped list by values .
Using sorted () with lambda
Lambda functions provide a flexible way to sort a zipped list by values, allowing custom sorting logic. This method is simple and useful for quick sorting tasks in Python.
Example:
Python
a = ['geeks', 'for', 'Geeks']
b= [3, 2, 1]
# Zipping the lists
zipped = zip(a,b)
# Converting to list
zipped = list(zipped)
# Sorting by second element
res = sorted(zipped, key=lambda x: x[1],)
print(res)
Output[('Geeks', 1), ('for', 2), ('geeks', 3)]
Explanation:
- This code zips lists a and b and sorts the pairs by the second element in ascending order using lambda function.
The sort() method sorts a zipped list in place, which means it modifies the list directly without creating a new one. While this is efficient for smaller datasets, it may be slower than using sorted() with operator.itemgetter() for larger datasets.
Example:
Python
a = ['geeks','for','Geeks']
b = [3, 2, 1]
# Zipping the lists
zipped = zip(a, b)
# Converting to list
zipped = list(zipped)
# Sorting in-place by second element
zipped.sort(key=lambda x: x[1])
print(zipped)
Output[('Geeks', 1), ('for', 2), ('geeks', 3)]
Explanation:
- This code zips lists a and b into pairs of corresponding elements.
- It sorts the pairs in-place by the second element in ascending order using lambda function.
Similar Reads
Ways to Sort List of Float Values - Python Given a list of float values, our task is to sort them in ascending order. For example, given [3.1, 2.4, 5.6, 1.8], the sorted output should be: [1.8, 2.4, 3.1, 5.6]Using sorted()Pythonâs built-in sorted() function sorts the list in ascending order by default. It is efficient and works well for most
2 min read
Sorting Python Dictionary With Lists as Values Python offers various methods to sort a dictionary with Lists as Values. This is a common task when dealing with data where you need to order elements based on different criteria. In this article, we will sort a dictionary with lists as values in Python. Sort a Dictionary with Lists as Values in Pyt
3 min read
Python - Sort Tuples by Total digits Given a Tuple List, perform sort on basis of total digits in tuple. Examples: Input : test_list = [(3, 4, 6, 723), (1, 2), (134, 234, 34)] Output : [(1, 2), (3, 4, 6, 723), (134, 234, 34)] Explanation : 2 < 6 < 8, sorted by increasing total digits. Input : test_list = [(1, 2), (134, 234, 34)]
6 min read
Python - Sort List by Dictionary values Sometimes while working with a Python dictionary, we can have problems in which we need to perform a sort of list according to the corresponding value in the dictionary. This can have applications in many domains, including data and web development. Let's discuss certain ways in which this task can
3 min read
Python - Sort Dictionary key and values List Sometimes, while working with Python dictionaries, we can have a problem in which we need to perform the sorting of it, wrt keys, but also can have a variation in which we need to perform a sort on its values list as well. Let's discuss certain way in which this task can be performed. Input : test_d
6 min read