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
# 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:
- 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
# 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)
OutputOriginal 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)
OutputOriginal 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 - 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
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
Python - Row with Minimum Sum in Matrix We can have an application for finding the lists with the minimum value and print it. This seems quite an easy task and may also be easy to code, but having shorthands to perform the same are always helpful as this kind of problem can come in web development. Method #1 : Using reduce() + lambda The
4 min read
Min and Max value in list of tuples-Python The task of finding the minimum and maximum values in a list of tuples in Python involves identifying the smallest and largest elements from each position (column) within the tuples. For example, given [(2, 3), (4, 7), (8, 11), (3, 6)], the first elements (2, 4, 8, 3) have a minimum of 2 and a maxim
3 min read
Python - Minimum element indices Sometimes, while working with Python lists, we can have a problem in which we intend to find the position of minimum element of list. This task is easy and discussed many times. But sometimes, we can have multiple minimum elements and hence multiple minimum positions. Letâs discuss a shorthand to ac
3 min read
Python | Row with Minimum element in Matrix We can have an application for finding the lists with the minimum value and print it. This seems quite an easy task and may also be easy to code, but sometimes we need to print the entire row containing it and having shorthands to perform the same are always helpful as this kind of problem can come
5 min read