Q:

Python program to find the index of minimum value record

belongs to collection: Python Tuple Programs

0

Example:

tuple = ("python", "includehelp", 43, 54.23)

Index Minimum Values Record

The problem consists of a list of tuples where each tuple consists of two values, one an index which is a string, and another an integer value. We will be creating a python program to find the index of minimum value record.

Input:
[('python', 51), ('Scala', 98), ('C/C++', 23)]

Output:
'C/C++'

To find the solution, we will iterate over the list and find the record with minimum value and print its index.

In Python, we can perform the task in multiple ways using one of the multiple methods that are present in the function.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Method 1:

One method to solve the problem is by using the itemgetter() method to get the index of each tuple and find the minimum value using the min() method and return its index.

# Program to find the index of 
# minimum value record

from operator import itemgetter
  
# initializing and printing list of records 
tupList = [('python', 51), ('Scala', 98), ('C/C++', 23)]
print ("The elements of List of Tuples are : " + str(tupList))
  
# finding the index of minimum value record 
minVal = min(tupList, key = itemgetter(1))[0]

print ("The index(name) of the record with minimum value is " + minVal)

Output:

The elements of List of Tuples are : [('python', 51), ('Scala', 98), ('C/C++', 23)]
The index(name) of the record with minimum value is C/C++

Method 2:

Another method to solve the problem is by using lambda function instead of itemgetter to extract the record's value and then return the index of the minimum value record.

# Program to find the index of 
# minimum value record

from operator import itemgetter
  
# initializing and printing list of records 
tupList = [('python', 51), ('Scala', 98), ('C/C++', 23)]
print ("The elements of List of Tuples are : " + str(tupList))
  
# finding the index of minimum value record 
minVal = min(tupList, key = lambda i : i[1])[0]

print ("The index(name) of the record with minimum value is " + minVal)

Output:

The elements of List of Tuples are : [('python', 51), ('Scala', 98), ('C/C++', 23)]
The index(name) of the record with minimum value is C/C++

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python Tuple Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Python program to extract rear element from list o... >>
<< Python program to find maximum value in record lis...