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 in each record value list
Next article icon

Python – Minimum in tuple list value

Last Updated : 09 Apr, 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 minimum of list as tuple attribute. Let’s discuss certain ways in which this task can be performed.

Method #1: Using sort() method + for loop

Python3




# Python3 code to demonstrate
# Minimum in tuple list value
 
# 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))
 
 
# Minimum of list as tuple attribute
res=[]
for i in test_list:
    a=i[1]
    a.sort()
    res.append((i[0],a[0]))
# 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 (log (M))),  where N and M are the lengths of the given test_list and the maximum size of the tuple that exists in the list respectively.
Auxiliary Space : O(max(N, M))

Method #2: 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 minimum of the list as a tuple attribute and list comprehension to iterate through the list. 

Python3




# Python3 code to demonstrate
# Minimum in tuple list value
# 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()
# Minimum of list as tuple attribute
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(N * M (log (M))),  where N and M are the lengths of the given test_list and the maximum size of the tuple that exists in the list respectively.
Auxiliary Space : O(max(N, M))

Method #3: 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
# Minimum in tuple list value
# 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()
# Minimum in tuple list value
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(N * M (log (M))),  where N and M are the lengths of the given test_list and the maximum size of the tuple that exists in the list respectively.
Auxiliary Space : O(max(N, M)), where N and M are the lengths of the given test_list and the maximum size of the tuple that exists in the list respectively.

Method 4: Using for loop + min()

  • Create an empty list called min_values that we will use to store the tuples with the minimum values.
  • Use a for loop to iterate through each tuple in the list of tuples. We will use the variable name tup to represent the current tuple, and unpack it into the variables key and lst.
  • Inside the for loop, use the built-in min() function to find the minimum value in the list associated with the current key.
  • Create a new tuple that consists of the key and the minimum value that we just found.
  • Append the new tuple to the min_values list.
  • After the for loop has finished iterating through all the tuples in the list, print out the resulting list of tuples that have the minimum values.

Below is the implementation:

Python3




# Python code to find the minimum value in a list of tuples
 
# initializing list of tuples
test_list = [('key1', [3, 4, 5]), ('key2', [1, 4, 2]), ('key3', [9, 3])]
 
# printing the original list
print("The original list is: " + str(test_list))
 
# using a for loop to find the minimum value in each list
# and store it as a tuple with the corresponding key
min_values = []
for key, lst in test_list:
    min_val = min(lst)
    min_values.append((key, min_val))
 
# printing the list of tuples with the minimum values
print("The list of tuples with the minimum values is: " + str(min_values))
 
 
Output
The original list is: [('key1', [3, 4, 5]), ('key2', [1, 4, 2]), ('key3', [9, 3])] The list of tuples with the minimum values is: [('key1', 3), ('key2', 1), ('key3', 3)]

Time Complexity: O(n * k), where n is the number of tuples in the list and k is the length of the largest list in the tuples. 
Auxiliary Space: O(n), where n is the number of tuples in the list.



Next Article
Python - Minimum in each record value list
author
manjeet_04
Improve
Article Tags :
  • Python
  • Python Programs
  • Python list-programs
Practice Tags :
  • python

Similar Reads

  • Python - Minimum in each record value list
    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 wa
    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 | List of tuples Minimum
    Sometimes, while working with Python records, we can have a problem in which we need to perform cross minimum of list of tuples. This kind of application is popular in web development domain. Let’s discuss certain ways in which this task can be performed. Method #1 : Using list comprehension + zip()
    4 min read
  • Min and Max value in list of tuples-Python
    The task of finding the minimum and maximum values in a list of tuples in Python involves identifying the smallest and largest elements from each position (column) within the tuples. For example, given [(2, 3), (4, 7), (8, 11), (3, 6)], the first elements (2, 4, 8, 3) have a minimum of 2 and a maxim
    3 min read
  • 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 | Maximum 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 maximum 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: U
    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
  • 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
  • Python - Extract Item with Maximum Tuple Value
    Sometimes, while working with Python dictionaries, we can have a problem in which we need to extract the item with maximum value of value tuple index. This kind of problem can have application in domains such as web development. Let's discuss certain ways in which this task can be performed. Input :
    5 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