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 | List of tuples Minimum
Next article icon

Python – Records Maxima in List of Tuples

Last Updated : 09 Apr, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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 max() + list comprehension + zip() 
This task can be performed using a combination of above functions. In this, we cumulate the like index elements, i.e columns using zip(), and then iterate through them using list comprehension and perform maximum using max().

Python3




# Python3 code to demonstrate working of
# Records Maxima in List of Tuples
# using list comprehension + max() + zip()
   
# initialize list
test_list = [(1, 2, 3), (6, 7, 6), (1, 6, 8)]
   
# printing original list
print("The original list : " + str(test_list))
   
# Records Maxima in List of Tuples
# using list comprehension + max() + zip()
res = [max(ele) for ele in zip(*test_list)]
   
# printing result
print("The Cumulative column maximum is : " + str(res))
 
 

Output:

The original list : [(1, 2, 3), (6, 7, 6), (1, 6, 8)] The Cumulative column maximum is : [6, 7, 8]

Time complexity: O(n), where n is the number of tuples in the list.

Auxiliary space: O(m), where m is the length of the longest tuple in the list.

 
Method #2 : Using zip() + map() + max() 
This method is similar to the above method. In this, the task performed by list comprehension is performed by map(), which extends the maximum of columns to zipped elements.

Python3




# Python3 code to demonstrate working of
# Records Maxima in List of Tuples
# using zip() + map() + max()
   
# initialize list
test_list = [(1, 2, 3), (6, 7, 6), (1, 6, 8)]
   
# printing original list
print("The original list : " + str(test_list))
   
# Records Maxima in List of Tuples
# using zip() + map() + max()
res = list(map(max, zip(*test_list)))
   
# printing result
print("The Cumulative column maximum is : " + str(res))
 
 

Output:

The original list : [(1, 2, 3), (6, 7, 6), (1, 6, 8)] The Cumulative column maximum is : [6, 7, 8]

Time complexity: O(n), where n is the number of tuples in the list.
Auxiliary space: O(m), where m is the length of the longest tuple in the list.

Method#3: Using Recursive method.

Python3




def find_maxima_recursive(lst, index, result):
    if not lst:
        return result
    else:
        result[index] = max(result[index], lst[0][index])
        return find_maxima_recursive(lst[1:], index, result)
 
def find_maxima(lst):
    if not lst:
        return []
    result = [0] * len(lst[0])
    for i in range(len(lst[0])):
        find_maxima_recursive(lst, i, result)
    return result
 
# initialize list
test_list = [(1, 2, 3), (6, 7, 6), (1, 6, 8)]
   
# printing original list
print("The original list : " + str(test_list))
res=find_maxima(test_list)
# printing result
print("The Cumulative column maximum is : " + str(res))
#this code contributed by tvsk
 
 
Output
The original list : [(1, 2, 3), (6, 7, 6), (1, 6, 8)] The Cumulative column maximum is : [6, 7, 8]

Time Complexity: O(n*m)

Auxiliary Space: O(n)

Method#4: using enumerate() + max() + zip() 

 Algorithm:

  1. Initialize an empty list “res”.
  2. Loop through each tuple in the list “test_list”.
  3. If it is the first tuple, add it to the “res” list as is.
  4. If it is not the first tuple, compare each element in the current tuple with its corresponding element in the “res” list and keep the maximum value.
  5. Update the “res” list with the maximum values.
  6. Return the “res” list.

Python3




# Python3 code to demonstrate working of
# Records Maxima in List of Tuples
# using enumerate() + max() + zip()
   
# initialize list
test_list = [(1, 2, 3), (6, 7, 6), (1, 6, 8)]
   
# printing original list
print("The original list : " + str(test_list))
   
# Records Maxima in List of Tuples
# using enumerate() + max() + zip()
res = []
for i, tup in enumerate(test_list):
    if i == 0:
        res = list(tup)
    else:
        res = [max(x, y) for x, y in zip(res, tup)]
   
# printing result
print("The Cumulative column maximum is : " + str(res))
#This code is contributed by Vinay Pinjala.
 
 
Output
The original list : [(1, 2, 3), (6, 7, 6), (1, 6, 8)] The Cumulative column maximum is : [6, 7, 8]

Time complexity: O(n*m), where n is the number of tuples in the list and m is the number of elements in each tuple. The loop runs n times, and the max() function runs m times for each iteration of the loop.

Auxiliary Space: O(m), where m is the number of elements in each tuple. The “res” list stores the maximum values for each element, so its length is equal to the number of elements in each tuple..

Method#5: Using numpy:

Algorithm:

1.Import the required library numpy.
2.Initialize the list of tuples.
3.Use the numpy library’s max function and pass the list of tuples as a parameter, and set the axis as 0. This will find the maximum value of each 4.element in each tuple for all the tuples.
5.Convert the numpy array to a list and print the result.

Python3




import numpy as np
 
# initialize list
test_list = [(1, 2, 3), (6, 7, 6), (1, 6, 8)]
# printing original list
print("The original list : " + str(test_list))
    
 
# finding maximum of each column using numpy's max() function and setting axis=0 to get column-wise maximums
max_list = np.max(test_list, axis=0)
 
# printing the resulting list as a Python list using tolist() method
print("The Cumulative column maximum is:", max_list.tolist())
#This  code is contributed by Jyothi pinjala.
 
 

Output: 

The original list : [(1, 2, 3), (6, 7, 6), (1, 6, 8)]
The Cumulative column maximum is: [6, 7, 8]

Time Complexity: O(N*M), where N is the number of tuples and M is the number of elements in each tuple.

Space Complexity: O(M), where M is the number of elements in each tuple.



Next Article
Python | List of tuples Minimum
author
manjeet_04
Improve
Article Tags :
  • Python
  • Python Programs
  • Python list-programs
Practice Tags :
  • python

Similar Reads

  • Sort Tuple of Lists in Python
    The task of sorting a tuple of lists involves iterating through each list inside the tuple and sorting its elements. Since tuples are immutable, we cannot modify them directly, so we must create a new tuple containing the sorted lists. For example, given a tuple of lists a = ([2, 1, 5], [1, 5, 7], [
    3 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 | 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
  • Python | Maximize Record list
    Sometimes, while working with Python records, we can have a problem in which we need to perform cross maximization 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 +
    6 min read
  • Python - Maximum of Similar Keys in Tuples
    Sometimes, while working with Python tuples, we can have a problem in which we need to perform maximum of all values of the equal keys in Tuple list. This kind of application in useful in many domains such as web development and day-day programming. Let's discuss certain ways in which this task can
    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
  • Mean of Tuple List - Python
    We are give list of tuples we need to find the mean of each tuples. For example, a = [(1, 2, 3), (4, 5, 6), (7, 8, 9)] we need to find mean of each tuples so that output should be (4.0, 5.0, 6.0). Using sum() and len()We can find the mean of a tuple list by summing corresponding elements using sum()
    2 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 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
  • Sort a List of Tuples by Second Item - Python
    The task of sorting a list of tuples by the second item is common when working with structured data in Python. Tuples are used to store ordered collections and sometimes, we need to sort them based on a specific element, such as the second item. For example, given the list [(1, 3), (4, 1), (2, 2)],
    2 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