Python | Index minimum value Record
Last Updated : 27 Apr, 2023
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
from operator import itemgetter test_list = [( 'Rash' , 143 ), ( 'Manjeet' , 200 ), ( 'Varsha' , 100 )] print ( "Original list : " + str (test_list)) res = min (test_list, key = itemgetter( 1 ))[ 0 ] 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
test_list = [( 'Rash' , 143 ), ( 'Manjeet' , 200 ), ( 'Varsha' , 100 )] print ( "Original list : " + str (test_list)) res = min (test_list, key = lambda i: i[ 1 ])[ 0 ] 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:
- Initialize a variable min_record to be the first element of the list, assuming it to be the minimum value record initially.
- Loop through the remaining elements of the list using a for loop, starting from the second element.
- 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.
- 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.
- Print the result.
Below is the implementation of the above approach:
Python3
test_list = [( 'Rash' , 143 ), ( 'Manjeet' , 200 ), ( 'Varsha' , 100 )] print ( "Original list: " + str (test_list)) min_record = test_list[ 0 ] for record in test_list[ 1 :]: if record[ 1 ] < min_record[ 1 ]: min_record = record res = min_record[ 0 ] 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
test_list = [( 'Rash' , 143 ), ( 'Manjeet' , 200 ), ( 'Varsha' , 100 )] print ( "Original list: " + str (test_list)) sorted_list = sorted (test_list, key = lambda x: x[ 1 ]) res = sorted_list[ 0 ][ 0 ] 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)
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