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 - Maximum element in Cropped List
Next article icon

Python | Maximum element in tuple list

Last Updated : 15 Mar, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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: Using max() + generator expression This is the most basic method to achieve the solution to this task. In this, we iterate over whole nested lists using generator expression and get the maximum element using max(). 

Python3
# Python3 code to demonstrate working of # Maximum element in tuple list # using max() + generator expression  # initialize list test_list = [(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)]  # printing original list print("The original list : " + str(test_list))  # Maximum element in tuple list # using max() + generator expression res = max(int(j) for i in test_list for j in i)  # printing result print("The Maximum element of list is : " + str(res)) 
Output : 
The original list : [(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)] The Maximum element of list is : 10

Method #2 : Using max() + map() + chain.from_iterable() The combination of above methods can also be used to perform this task. In this, the extension of finding maximum is done by combination of map() and from_iterable(). 

Python3
# Python3 code to demonstrate working of # Maximum element in tuple list # using max() + map() + chain.from_iterable() from itertools import chain  # initialize list test_list = [(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)]  # printing original list print("The original list : " + str(test_list))  # Maximum element in tuple list # using max() + map() + chain.from_iterable() res = max(map(int, chain.from_iterable(test_list)))  # printing result print("The Maximum element of list is : " + str(res)) 
Output : 
The original list : [(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)] The Maximum element of list is : 10

Method #3: Using list comprehension method 

Python3
test_list = [(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)]  x=[j for i in test_list for j in i ] print(max(x)) 

Output
10

Method #4: Using enumerate function

Python3
test_list = [(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)]  x=[j for a,i in enumerate(test_list) for j in i ] print(max(x)) 

Output
10

Method #5: Using itemgetter

Python3
from operator import itemgetter my_list = [('Will', 23), ('Jane', 21), ('El', 24), ('Min', 101)]   my_result = max(my_list, key = itemgetter(1))[1]  print ("The maximum value is : ") print(my_result) 

Output
The maximum value is :  101

Method #6: Using the lambda function

Python3
test_list = [(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)] x=[j for i in test_list for j in i ] x=list(filter(lambda i: (i),x)) print(max(x)) 

Output
10

Method #7:  Using extend() and max() methods

Python3
# Python3 code to demonstrate working of # Maximum element in tuple list  # initialize list test_list = [(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)]  # printing original list print("The original list : " + str(test_list))  # Maximum element in tuple list x=[] for i in test_list:     x.extend(list(i)) res=max(x)  # printing result print("The Maximum element of list is : " + str(res)) 

Output
The original list : [(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)] The Maximum element of list is : 10

Method #8: Using numpy.max()

Note: Install numpy module using command "pip install numpy"


You can use numpy.max() to find the maximum element in the tuple list.

Python3
import numpy as np  # initialize list test_list = [(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)]  # printing original list print("The original list : " + str(test_list))  # Maximum element in tuple list # using numpy.max() res = np.max(test_list)  # printing result print("The Maximum element of list is : " + str(res)) #This code is contributed by Edula Vinay Kumar Reddy 

Output:

The original list : [(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)]
The Maximum element of list is : 10

method has the time complexity of O(n) where n is the number of elements in the list. Auxiliary Space is O(1)

Method #9:Using reduce

Algorithm:

  1. Initialize a list of tuples.
  2. Flatten the list of tuples into a single list of integers using reduce.
  3. Remove any 0 or False values from the flattened list using filter.
  4. Find the maximum value in the filtered list using max.
  5. Print the maximum value.
Python3
from functools import reduce  test_list = [(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)]  # printing original list print("The original list : " + str(test_list)) # Use reduce to flatten the list of tuples into a single list of integers flat_list = reduce(lambda acc, x: acc + list(x), test_list, [])  # Use filter to remove any 0 or False values from the list filtered_list = list(filter(lambda x: x, flat_list))  # Find the maximum value in the filtered list res = max(filtered_list) # printing result print("The Maximum element of list is : " + str(res)) #This code is contributed by Vinay Pinjala. 

Output
The original list : [(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)] The Maximum element of list is : 10 

Time complexity is O(n)

In the case of the provided code, the time complexity is O(n), where n is the total number of elements in the list of tuples. This is due to the fact that the most time-consuming operations in the code involve iterating through the list of tuples, flattening it using reduce, and filtering out any 0 or False values using filter. These operations take linear time as a function of the input size, and therefore contribute to the overall O(n) time complexity. 

 space complexity is also O(n)

where n is the total number of elements in the list of tuples. This is due to the fact that the code requires a certain amount of memory to store the list of tuples, flatten it using reduce, and filter out any 0 or False values using filter. These operations require memory proportional to the size of the input, and therefore contribute to the overall O(n) space complexity.


Next Article
Python - Maximum element in Cropped List
author
manjeet_04
Improve
Article Tags :
  • Python
  • Python Programs
  • Python tuple-programs
Practice Tags :
  • python

Similar Reads

  • 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 - Maximum element in Cropped List
    Sometimes, while working with Python, we can have a problem in which we need to get maximum of list. But sometimes, we need to get this for between custom indices. This can be need of any domain be it normal programming or web development. Let's discuss certain ways in which this task can be perform
    4 min read
  • Python - K Maximum elements with Index in List
    GIven a List, extract K Maximum elements with their indices. Input : test_list = [5, 3, 1, 4, 7, 8, 2], K = 2 Output : [(4, 7), (5, 8)] Explanation : 8 is maximum on index 5, 7 on 4th. Input : test_list = [5, 3, 1, 4, 7, 10, 2], K = 1 Output : [(5, 10)] Explanation : 10 is maximum on index 5. Method
    4 min read
  • Python - Ranged Maximum Element in String List
    Sometimes, while working with Python data, we can have a problem in which we have data in form of String List and we require to find the maximum element in that data, but that also in a certain range of indices. This is quite peculiar problem but can have application in data domains. Let's discuss c
    5 min read
  • Python - Maximum frequency in Tuple
    Sometimes, while working with Python tuples, we can have a problem in which we need to find the maximum frequency element in the tuple. Tuple, being quite a popular container, this type of problem is common across the web development domain. Let's discuss certain ways in which this task can be perfo
    5 min read
  • Python - Maximum and Minimum K elements in Tuple
    Sometimes, while dealing with tuples, we can have problem in which we need to extract only extreme K elements, i.e maximum and minimum K elements in Tuple. This problem can have applications across domains such as web development and Data Science. Let's discuss certain ways in which this problem can
    8 min read
  • Python - Maximum of K element in other list
    Given two lists, extract maximum of elements with similar K in corresponding list. Input : test_list1 = [4, 3, 6, 2, 8], test_list2 = [3, 6, 3, 4, 3], K = 3 Output : 8 Explanation : Elements corresponding to 3 are, 4, 6, and 8, Max. is 8. Input : test_list1 = [10, 3, 6, 2, 8], test_list2 = [5, 6, 5,
    4 min read
  • Python - Elements Maximum till current index in List
    Given list with elements, extract element if it's the maximum element till current index. Input : test_list = [4, 6, 7, 8] Output : [4, 6, 7, 8] Explanation : All elements are maximum till their index. Input : test_list = [6, 7, 3, 6, 8, 7] Output : [7, 8] Explanation : 7 and 8 are maximum till thei
    7 min read
  • Python | Positions of maximum element in list
    Sometimes, while working with Python lists, we can have a problem in which we intend to find the position of maximum element of list. This task is easy and discussed many times. But sometimes, we can have multiple maximum elements and hence multiple maximum positions. Let's discuss a shorthand to ac
    3 min read
  • Python - Maximum element till K value
    One of the problem that is basically a subproblem for many complex problems, finding maximum number till a certain number in list in python, is commonly encountered and this particular article discusses possible solutions to this particular problem. Method #1 : Naive method The most common way this
    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