Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Python - Sort Dictionary key and values List
Next article icon

Python | Ways to sort a zipped list by values

Last Updated : 26 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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.


Next Article
Python - Sort Dictionary key and values List
author
garg_ak0109
Improve
Article Tags :
  • Python
  • Python Programs
  • Python list-programs
  • python-list
Practice Tags :
  • python
  • python-list

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 mos
    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
  • Python | Sort lists in tuple
    Sometimes, while working with Python tuples, we can have a problem in which we need to sort the tuples which constitutes of lists and we need to sort each of them. Let's discuss certain ways in which this task can be performed. Method #1 : Using tuple() + sorted() + generator expression This task ca
    4 min read
  • Python - Sort Dictionary by Values Summation
    Give a dictionary with value lists, sort the keys by summation of values in value list. Input : test_dict = {'Gfg' : [6, 7, 4], 'best' : [7, 6, 5]} Output : {'Gfg': 17, 'best': 18} Explanation : Sorted by sum, and replaced. Input : test_dict = {'Gfg' : [8], 'best' : [5]} Output : {'best': 5, 'Gfg':
    4 min read
  • Sort Python Dictionary by Value
    Python dictionaries are versatile data structures that allow you to store key-value pairs. While dictionaries maintain the order of insertion. sorting them by values can be useful in various scenarios. In this article, we'll explore five different methods to sort a Python dictionary by its values, a
    3 min read
  • Python - Sort Dictionary by Values and Keys
    Given a dictionary, sort according to descended values, if similar values, then by keys lexicographically. Input : test_dict = {"gfg" : 1, "is" : 1, "best" : 1, "for" : 1, "geeks" : 1} Output : {"best" : 1, "is" : 1, "for" : 1, "geeks" : 1, "gfg" : 1} Explanation : All values are equal, hence lexico
    3 min read
  • Sort Tuple of Lists in Python
    The task of sorting a tuple of lists involves iterating through each list inside the tuple and sorting its elements. Since tuples are immutable, we cannot modify them directly, so we must create a new tuple containing the sorted lists. For example, given a tuple of lists a = ([2, 1, 5], [1, 5, 7], [
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences