Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Python | Minimum K records of Nth index in tuple list
Next article icon

Python – Minimum in each record value list

Last Updated : 16 May, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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 ways in which this task can be performed. 

Method #1 : Using list comprehension + min() This particular problem can be solved using list comprehension combined with the min function in which we use min function to find the min of list as a tuple attribute and list comprehension to iterate through the list. 

Python3




# Python3 code to demonstrate
# Record Value list Minimum
# using list comprehension + min()
 
# initializing list
test_list = [('key1', [3, 4, 5]), ('key2', [1, 4, 2]), ('key3', [9, 3])]
 
# printing original list
print("The original list : " + str(test_list))
 
# using list comprehension + min()
# Record Value list Minimum
res = [(key, min(lst)) for key, lst in test_list]
 
# print result
print("The list tuple attribute minimum is : " + str(res))
 
 
Output : 
The original list : [('key1', [3, 4, 5]), ('key2', [1, 4, 2]), ('key3', [9, 3])] The list tuple attribute minimum is : [('key1', 3), ('key2', 1), ('key3', 3)]

Time complexity: O(M^N) as the number of combinations generated is M choose N.
Auxiliary space: O(M^N) as the size of the resultant list is also M choose N.

Method #2: Using map + lambda + min() The above problem can also be solved using the map function to extend the logic to the whole list and min function can perform the similar task as the above method. 

Python3




# Python3 code to demonstrate
# Record Value list Minimum
# using map() + lambda + min()
 
# initializing list
test_list = [('key1', [3, 4, 5]), ('key2', [1, 4, 2]), ('key3', [9, 3])]
 
# printing original list
print("The original list : " + str(test_list))
 
# using map() + lambda + min()
# Record Value list Minimum
res = list(map(lambda x: (x[0], min(x[1])), test_list))
 
# print result
print("The list tuple attribute minimum is : " + str(res))
 
 
Output : 
The original list : [('key1', [3, 4, 5]), ('key2', [1, 4, 2]), ('key3', [9, 3])] The list tuple attribute minimum is : [('key1', 3), ('key2', 1), ('key3', 3)]

Time complexity: O(M^N) as the number of combinations generated is M choose N.
Auxiliary space: O(M^N) as the size of the resultant list is also M choose N.

Method #3 : Using reduce()

Python3




# Using reduce
 
from functools import reduce
 
# Initializing list
test_list = [('key1', [3, 4, 5]), ('key2', [1, 4, 2]), ('key3', [9, 3])]
 
# Finding minimum in each sublist using reduce
result = [(key, reduce(lambda x, y: x if x<y else y, lst)) for key, lst in test_list]
 
# Printing the final result
print("The list tuple attribute minimum is:", result)
 
 
Output
The list tuple attribute minimum is: [('key1', 3), ('key2', 1), ('key3', 3)]

Time complexity: O(n^2)
Space complexity: O(n)

Explanation:
In this approach, we use the reduce function from the functools library to find the minimum in each sublist. We iterate through the list of tuples and use the reduce function to find the minimum of each sublist. Finally, we store the result in a new list of tuples and print the result.

Method 4: Use a loop to iterate through the list of tuples and finding the minimum value for each tuple’s value list.

Python3




# Python3 code to demonstrate
# Record Value list Minimum
# using for loop
 
# initializing list
test_list = [('key1', [3, 4, 5]), ('key2', [1, 4, 2]), ('key3', [9, 3])]
 
# printing original list
print("The original list : " + str(test_list))
 
# using for loop
# Record Value list Minimum
res = []
for key, lst in test_list:
    min_val = min(lst)
    res.append((key, min_val))
 
# print result
print("The list tuple attribute minimum is : " + str(res))
 
 
Output
The original list : [('key1', [3, 4, 5]), ('key2', [1, 4, 2]), ('key3', [9, 3])] The list tuple attribute minimum is : [('key1', 3), ('key2', 1), ('key3', 3)]

Time Complexity: O(n * m), where n is the length of the list and m is the length of the longest value list.
Auxiliary Space: O(n), where n is the length of the list.

Method 5: Uses a dictionary comprehension

This method uses a dictionary comprehension to create a dictionary where the keys are the first elements of the tuples and the values are the minimum values of the second elements of the tuples. Then, it converts the dictionary to a list of tuples.

Python3




# Python3 code to demonstrate
# Record Value list Minimum
# using dictionary comprehension
 
# initializing list
test_list = [('key1', [3, 4, 5]), ('key2', [1, 4, 2]), ('key3', [9, 3])]
 
# printing original list
print("The original list : " + str(test_list))
 
# using dictionary comprehension
# Record Value list Minimum
res_dict = {key: min(lst) for key, lst in test_list}
res = list(res_dict.items())
 
# print result
print("The list tuple attribute minimum is : " + str(res))
 
 
Output
The original list : [('key1', [3, 4, 5]), ('key2', [1, 4, 2]), ('key3', [9, 3])] The list tuple attribute minimum is : [('key1', 3), ('key2', 1), ('key3', 3)]

Time complexity: O(n)
Auxiliary space: O(n)

Method #6: Using itertools.chain() and min()

  • Import itertools module.
  • Use itertools.chain() function to flatten the list of lists into a single list.
  • Use min() function to find the minimum value in the flattened list.
  • Repeat the above steps for each list of tuples in the given list and store the minimum value in a list.
  • Create a new list of tuples with keys from the original list and minimum values from the list created in step 4.

Python3




import itertools
 
# initializing list
test_list = [('key1', [3, 4, 5]), ('key2', [1, 4, 2]), ('key3', [9, 3])]
 
# printing original list
print("The original list : " + str(test_list))
 
# using itertools.chain() and min()
# Record Value list Minimum
res = []
for key, lst in test_list:
    min_val = min(itertools.chain(*[lst]))
    res.append((key, min_val))
 
# print result
print("The list tuple attribute minimum is : " + str(res))
 
 
Output
The original list : [('key1', [3, 4, 5]), ('key2', [1, 4, 2]), ('key3', [9, 3])] The list tuple attribute minimum is : [('key1', 3), ('key2', 1), ('key3', 3)]

Time complexity: O(nm) where n is the number of tuples in the list and m is the length of the longest list in the tuples.
Auxiliary space: O(nm) to store the flattened list.



Next Article
Python | Minimum K records of Nth index in tuple list
author
manjeet_04
Improve
Article Tags :
  • Python
  • Python Programs
  • Python list-programs
Practice Tags :
  • python

Similar Reads

  • 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. Le
    4 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 - Maximum value in record list as tuple attribute
    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 max of a list as a tuple attribute. Let’s discuss certai
    8 min read
  • Python | Minimum K records of Nth index in tuple list
    Sometimes, while working with data, we can have a problem in which we need to get the minimum of elements filtered by the Nth element of record. This has a very important utility in web development domain. Let’s discuss certain ways in which this task can be performed. Method #1 : Using filter() + l
    9 min read
  • Python - Maximum record value key in dictionary
    Sometimes, while working with dictionary records, we can have a problem in which we need to find the key with maximum value of a particular key of nested records in list. This can have applications in domains such as web development and Machine Learning. Lets discuss certain ways in which this task
    6 min read
  • Python - Column Minimum in Tuple list
    Sometimes, while working with records, we can have a problem in which we need to find min of all the columns of a container of lists which are tuples. This kind of application is common in web development domain. Let’s discuss certain ways in which this task can be performed. Method #1 : Using min()
    6 min read
  • Python | Minimum element in tuple list
    Sometimes, while working with data in form of records, we can have a problem in which we need to find the minimum element of all the records received. This is a very common application that can occur in Data Science domain. Let's discuss certain ways in which this task can be performed. Method #1 :
    5 min read
  • Python - Records Maxima in List of Tuples
    Sometimes, while working with records, we can have a problem in which we need to the maximum all the columns of a container of lists that are tuples. This kind of application is common in the web development domain. Let’s discuss certain ways in which this task can be performed. Method #1 : Using ma
    5 min read
  • Maximum and Minimum value from two lists - Python
    Finding the maximum and minimum values from two lists involves comparing all elements to determine the highest and lowest values. For example, given two lists [3, 5, 7, 2, 8] and [4, 9, 1, 6, 0], we first examine all numbers to identify the largest and smallest. In this case, 9 is the highest value
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences