Python program to find the index of minimum value record

In this program, we are given a list of tuples where each tuple is a record consisting a name and a value associated with it. We need to create a Python program to find the index of the minimum value record.
Submitted by Shivang Yadav, on November 29, 2021

Extracting values based on a certain condition in python is commonly used. A python record consisting of certain values is extracted from the collection using conditions like maximum, minimum, even, odd values of a certain value of the record. In this problem, we will be extracting the index of the minimum value record.

Before going further with the problem, let's recap some basic topics that will help in understanding the solution.

Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.

Tuples in Python is a collection of items similar to list with the difference that it is ordered and immutable.

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.

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++

Python Tuple Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.