Wednesday, November 27, 2024
Google search engine
HomeLanguagesPython | Index minimum value Record

Python | Index minimum value Record

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)

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments