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 - Records Maxima in List of Tuples
Next article icon

Python | Maximize Record list

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

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 + zip() The combination of above functionalities can be used to perform this particular task. In this, we iterate through the list using list comprehension and the maximization across lists is performed with help of zip(). 

Python3
# Python3 code to demonstrate working of # Maximize Record list # using list comprehension + zip()  # initialize lists test_list1 = [(2, 4), (6, 7), (5, 1)] test_list2 = [(5, 4), (8, 10), (8, 14)]  # printing original lists print("The original list 1 : " + str(test_list1)) print("The original list 2 : " + str(test_list2))  # Maximize Record list # using list comprehension + zip() res = [(max(x[0], y[0]), max(x[1], y[1])) for x, y in zip(test_list1, test_list2)]  # printing result print("The Maximums across lists is : " + str(res)) 

Time Complexity: O(n) where n is the number of elements in the list “test_list”. list comprehension + zip() performs n number of operations.
Auxiliary Space: O(n), extra space is required where n is the number of elements in the list

  Method #2 : Using max() + zip() + map() This is yet another way to perform this task. This is similar to above method, the difference is that maximization is performed by inbuilt function and extending logic to each element is done by map(). 

Python3
# Python3 code to demonstrate working of # Maximize Record list # using max() + zip() + map()  # initialize lists test_list1 = [(2, 4), (6, 7), (5, 1)] test_list2 = [(5, 4), (8, 10), (8, 14)]  # printing original lists print("The original list 1 : " + str(test_list1)) print("The original list 2 : " + str(test_list2))  # Maximize Record list # using max() + zip() + map() res = [tuple(map(max, zip(a, b))) for a, b in zip(test_list1, test_list2)]  # printing result print("The maximums across lists is : " + str(res)) 

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 : use the itertools.starmap() function to find the maximums across the lists. This function applies the provided function to the elements of the input iterable(s) and returns an iterator yielding the results.

Here is an example:

Python3
from itertools import starmap  # initialize lists test_list1 = [(2, 4), (6, 7), (5, 1)] test_list2 = [(5, 4), (8, 10), (8, 14)]  # printing original lists print("The original list 1 : ", test_list1) print("The original list 2 : ", test_list2)  # Maximize Record list res = list(starmap(lambda x, y: (max(x[0], y[0]), max(x[1], y[1])), zip(test_list1, test_list2)))  # printing result print("The Maximums across lists is : ", res) #This code is contributed by Edula Vinay Kumar Reddy 

Output
The original list 1 :  [(2, 4), (6, 7), (5, 1)] The original list 2 :  [(5, 4), (8, 10), (8, 14)] The Maximums across lists is :  [(5, 4), (8, 10), (8, 14)]

This approach is unique as it uses the itertools.starmap() function to apply the max function to the elements of the input iterable(s) and returns an iterator yielding the results. Time and Auxiliary Space is O(n) where n is the length of the lists.

 Method #4: using map() and lambda:

Python3
test_list1 = [(2, 4), (6, 7), (5, 1)] test_list2 = [(5, 4), (8, 10), (8, 14)] # printing original lists print("The original list 1 : ", test_list1) print("The original list 2 : ", test_list2) res = list(map(lambda x, y: tuple(map(max, x, y)), test_list1, test_list2)) print("The maximums across lists is : " + str(res)) #This code is contributed by Jyothi pinjala 

Output
The original list 1 :  [(2, 4), (6, 7), (5, 1)] The original list 2 :  [(5, 4), (8, 10), (8, 14)] The maximums across lists is : [(5, 4), (8, 10), (8, 14)]

Time Complexity: O(N)
Auxiliary Space:  O(N)

Method #5: Using a for loop

Step-by-step approach:

  • Initialize an empty list called res.
  • Use a for loop to iterate over the indices of test_list1 and test_list2 simultaneously.
  • For each iteration, find the maximum value between the corresponding tuples in test_list1 and test_list2 using the max() function.
  • Append the resulting tuple to the res list.
  • After all iterations are complete, print the resulting list res.
Python3
# Python3 code to demonstrate working of # Maximize Record list # using a for loop  # initialize lists test_list1 = [(2, 4), (6, 7), (5, 1)] test_list2 = [(5, 4), (8, 10), (8, 14)]  # printing original lists print("The original list 1 : " + str(test_list1)) print("The original list 2 : " + str(test_list2))  # Maximize Record list using a for loop res = [] for i in range(len(test_list1)):     max_tuple = (max(test_list1[i][0], test_list2[i][0]), max(test_list1[i][1], test_list2[i][1]))     res.append(max_tuple)  # printing result print("The Maximums across lists is : " + str(res)) 

Output
The original list 1 : [(2, 4), (6, 7), (5, 1)] The original list 2 : [(5, 4), (8, 10), (8, 14)] The Maximums across lists is : [(5, 4), (8, 10), (8, 14)]

Time complexity: O(n), where n is the length of the input lists.
Auxiliary space: O(n), where n is the length of the input lists.

Method #6: Using NumPy

Explanation:

  • First, we import the NumPy library.
  • Then, we convert the lists to NumPy arrays using the np.array() method.
  • We then perform the element-wise maximum operation using the np.maximum() method.
  • Finally, we convert the resulting NumPy array back to a list using the tolist() method and print the result.
Python3
import numpy as np  # initialize lists test_list1 = [(2, 4), (6, 7), (5, 1)] test_list2 = [(5, 4), (8, 10), (8, 14)]  # convert lists to NumPy arrays arr1 = np.array(test_list1) arr2 = np.array(test_list2)  # perform element-wise maximum operation res = np.maximum(arr1, arr2)  # printing result print("The Maximums across lists is : " + str(res.tolist())) 

Output:

The Maximums across lists is : [[5, 4], [8, 10], [8, 14]]

Time complexity: O(n), where n is the length of the input lists.
Auxiliary space: O(n), where n is the length of the input lists.


Next Article
Python - Records Maxima in List of Tuples
author
manjeet_04
Improve
Article Tags :
  • Python
  • Python Programs
  • Python list-programs
Practice Tags :
  • python

Similar Reads

  • 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
  • Python - Maximum Sum Record
    Sometimes, while working with data, we might have a problem in which we need to find maximum sum between available pairs in list. This can be application to many problems in mathematics domain. Let’s discuss certain ways in which this task can be performed. Method #1 : Using max() + list comprehensi
    3 min read
  • Python - Equable Minimial Records
    Sometimes, while working with Python records, we can have a problem in which we need to extract one of the records that are equal to certain index, which is minimal of other index. This kind of problem occurs in domains such as web development. Let's discuss certain ways in which this task can be pe
    5 min read
  • Python - Row with Maximum Record Element
    Sometimes, while working with Python Records, we can have a problem in which we need to find the row with maximum record element. This kind of problem can come in domains of web development and day-day programming. Let's discuss certain ways in which this task can be performed. Input : test_list = [
    7 min read
  • Python - Maximum in Row Range
    Given a range and a Matrix, extract the maximum element out of that range of rows. Input : test_list = [[4, 3, 6], [9, 1, 3], [4, 5, 2], [9, 10, 3], [5, 9, 12], [3, 14, 2]], i, j = 2, 5 Output : 12 Explanation : Checks for rows 2, 3 and 4, maximum element is 12. Input : test_list = [[4, 3, 6], [9, 1
    5 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
  • Python - Maximum Quotient Pair in List
    Sometimes, we need to find the specific problem of getting the pair which yields the maximum Quotient, this can be solved by sorting and getting the first and last elements of the list. But in some case, we don’t with to change the ordering of list and perform some operation in a similar list withou
    5 min read
  • Concatenate All Records - Python
    The task of concatenating all records in Python involves combining elements from a list, typically strings, into a single unified string. The goal is to concatenate each individual element, ensuring that the result is a continuous string without spaces or delimiters, unless specified. For example, g
    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 | Maximize alternate element List
    The problem of getting maximum of a list is quite generic and we might some day face the issue of getting the maximum of alternate elements and get the list of 2 elements containing maximum of alternate elements. Let’s discuss certain ways in which this can be performed. Method #1 : Using list compr
    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