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 - Maximum record value key in dictionary
Next article icon

Python | Index minimum value Record

Last Updated : 27 Apr, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

In Python, we can bind structural information in the form of tuples and then can retrieve the same, and has manyfold applications. But sometimes we require the information of a tuple corresponding to a minimum value of another tuple index. This functionality has many applications such as ranking. Let us discuss certain ways in which this can be achieved. 

Method #1 : Using min() + operator.itemgetter()

We can get the minimum of the corresponding tuple index from a list using the key ‘itemgetter’ index provided and then mention the index information required using index specification at the end.

Python3




# Python 3 code to demonstrate
# Index minimum value Record
# using min() + itemgetter()
 
from operator import itemgetter
 
# initializing list
test_list = [('Rash', 143), ('Manjeet', 200), ('Varsha', 100)]
 
# printing original list
print("Original list : " + str(test_list))
 
# using min() + itemgetter()
# Index minimum value Record
res = min(test_list, key=itemgetter(1))[0]
 
# Printing result
print("The name with minimum score is : " + res)
 
 
Output : 
Original list : [('Rash', 143), ('Manjeet', 200), ('Varsha', 100)] The name with minimum score is : Varsha

Method #2: Using min() + lambda 

This method is almost similar to the method discussed above, just the difference is the specification and processing of the target tuple index for minimum is done by lambda function. This improved readability of code. 

Python3




# Python 3 code to demonstrate
# Index minimum value Record
# using min() + lambda
 
# initializing list
test_list = [('Rash', 143), ('Manjeet', 200), ('Varsha', 100)]
 
# printing original list
print("Original list : " + str(test_list))
 
# Index minimum value Record
# using min() + lambda
res = min(test_list, key=lambda i: i[1])[0]
 
# printing result
print("The name with minimum score is : " + res)
 
 
Output : 
Original list : [('Rash', 143), ('Manjeet', 200), ('Varsha', 100)] The name with minimum score is : Varsha

Time Complexity: O(n)
Auxiliary Space: O(n), where n is the length of the list.

Method #3: Using a for loop to iterate through the list and keep track of the minimum value record

Approach:

  1. Initialize a variable min_record to be the first element of the list, assuming it to be the minimum value record initially.
  2. Loop through the remaining elements of the list using a for loop, starting from the second element.
  3. For each element, compare its second value (i.e., the score) with the second value of min_record using an if statement. If the current element has a lower score than min_record, update min_record to be the current element.
  4. After the loop finishes, min_record will contain the element with the lowest score. Extract the name from min_record using indexing and assign it to a variable res.
  5. Print the result.

Below is the implementation of the above approach:

Python3




# Python 3 code to demonstrate
# Index minimum value Record
# using a for loop
 
# initializing list
test_list = [('Rash', 143), ('Manjeet', 200), ('Varsha', 100)]
 
# printing original list
print("Original list: " + str(test_list))
 
# using a for loop
# Index minimum value Record
min_record = test_list[0]
 
for record in test_list[1:]:
    if record[1] < min_record[1]:
        min_record = record
res = min_record[0]
 
# printing result
print("The name with minimum score is: " + res)
 
 
Output
Original list: [('Rash', 143), ('Manjeet', 200), ('Varsha', 100)] The name with minimum score is: Varsha

Time complexity: O(n) where n is the length of the input list. This is because we need to iterate through each element of the list once.
Auxiliary space: O(1). We are only using a constant amount of extra space to store min_record and res.

Method #4: Usingbuilt-in sorted() function

This method sorts the list in ascending order based on the score and returns the name of the first record (with the lowest score).

Python3




# Using the built-in sorted() function
 
# Input list
test_list = [('Rash', 143), ('Manjeet', 200), ('Varsha', 100)]
 
print("Original list: " + str(test_list))
 
# Sorting list
sorted_list = sorted(test_list, key=lambda x: x[1])
 
res = sorted_list[0][0]
 
# Printing list
print("The name with minimum score is: " + res)
 
 
Output
Original list: [('Rash', 143), ('Manjeet', 200), ('Varsha', 100)] The name with minimum score is: Varsha

Time complexity: O(n*log(n)) where n is the length of the input list. This is because we need to iterate through each element of the list once.
Auxiliary space: O(1)



Next Article
Python - Maximum record value key in dictionary
author
manjeet_04
Improve
Article Tags :
  • Python
  • Python Programs
  • Python tuple-programs
Practice Tags :
  • python

Similar Reads

  • Python - Minimum in each record value list
    Many times, while dealing with containers in any language we come across lists of tuples in different forms, tuples in themselves can have sometimes more than native datatypes and can have list as their attributes. This article talks about the min of list as tuple attribute. Let’s discuss certain wa
    6 min read
  • Python - Minimum in tuple list value
    Many times, while dealing with containers in any language we come across lists of tuples in different forms, tuples in themselves can have sometimes more than native datatypes and can have list as their attributes. This article talks about the minimum of list as tuple attribute. Let’s discuss certai
    5 min read
  • Python - Records with Value at K index
    Sometimes, while working with records, we might have a problem in which we need to find all the tuples of elements for a particular value at a particular Kth position of tuple. This seems to be a peculiar problem but while working with many keys in records, we encounter this problem. Let’s discuss c
    8 min read
  • Python - Maximum record value key in dictionary
    Sometimes, while working with dictionary records, we can have a problem in which we need to find the key with maximum value of a particular key of nested records in list. This can have applications in domains such as web development and Machine Learning. Lets discuss certain ways in which this task
    6 min read
  • Python - Find minimum k records from tuple list
    Sometimes, while working with data, we can have a problem in which we have records and we require to find the lowest K scores from it. This kind of application is popular in web development domain. Let’s discuss certain ways in which this problem can be solved. Method #1 : Using sorted() + lambda Th
    6 min read
  • Python - Minimum value key assignment
    Given two dictionaries, the task is to write a Python program to assign minimum values for matching keys from both dictionaries. Examples: Input : test_dict1 = {"gfg" : 1, "is" : 7, "best" : 8}, test_dict2 = {"gfg" : 2, "is" : 2, "best" : 10}Output : {"gfg" : 1, "is" : 2, "best" : 8}Explanation : Mi
    5 min read
  • Python | Minimum K records of Nth index in tuple list
    Sometimes, while working with data, we can have a problem in which we need to get the minimum of elements filtered by the Nth element of record. This has a very important utility in web development domain. Let’s discuss certain ways in which this task can be performed. Method #1 : Using filter() + l
    9 min read
  • Python | Record Point with Minimum difference
    Sometimes, while working with data, we might have a problem in which we need to find minimum difference between available pairs in list. This can be application to many problems in mathematics domain. Let’s discuss certain ways in which this task can be performed.Method #1 : Using min() + list compr
    2 min read
  • Minimum Value Keys in Dictionary - Python
    We are given a dictionary and need to find all the keys that have the minimum value among all key-value pairs. The goal is to identify the smallest value in the dictionary and then collect every key that matches it. For example, in {'a': 3, 'b': 1, 'c': 2, 'd': 1}, the minimum value is 1, so the res
    4 min read
  • Python - Equable Minimial Records
    Sometimes, while working with Python records, we can have a problem in which we need to extract one of the records that are equal to certain index, which is minimal of other index. This kind of problem occurs in domains such as web development. Let's discuss certain ways in which this task can be pe
    5 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