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 Tutorial | Learn Python Programming Language Python Tutorial â Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly.Python is:A high-level language, used in web development, data science, automatio
10 min read
Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list
10 min read
Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co
11 min read
Enumerate() in Python enumerate() function adds a counter to each item in a list or other iterable. It turns the iterable into something we can loop through, where each item comes with its number (starting from 0 by default). We can also turn it into a list of (number, item) pairs using list().Let's look at a simple exam
3 min read
Python Data Types Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes
9 min read
Python Introduction Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien
3 min read
Input and Output in Python Understanding input and output operations is fundamental to Python programming. With the print() function, we can display output in various formats, while the input() function enables interaction with users by gathering input during program execution. Taking input in PythonPython input() function is
8 min read