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

Maximum and Minimum value from two lists – Python

Last Updated : 07 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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 and 0 is the lowest. This process ensures that all elements are considered, and the extreme values are correctly identified. Let’s explore different methods to achieve this.

Using max() and min()

max() function returns the largest element from an iterable, while min() returns the smallest. By applying max() and min() twice, once for each list and then on the results—we determine the overall maximum and minimum values across both lists. Example:

Python
a = [3, 5, 7, 2, 8] b = [4, 9, 1, 6, 0]  c = max(max(a), max(b)) d = min(min(a), min(b))  print(c,d) 

Output
9 0 

Explanation: This code finds the maximum and minimum values from each list using max() and min(), then applies max() and min() again to get the overall maximum and minimum across both lists.

Table of Content

  • Using heapq.nlargest() and heapq.nsmallest()
  • Using itertools.chain() with max() and min()
  • Using list merging and sorting

Using heapq.nlargest() and heapq.nsmallest()

The heapq module provides functions like nlargest() and nsmallest(), which efficiently find the largest and smallest elements from an iterable. Here, heapq.nlargest(1, a + b)[0] extracts the single largest value and heapq.nsmallest(1, a + b)[0] extracts the smallest. Example:

Python
import heapq  a = [3, 5, 7, 2, 8] b = [4, 9, 1, 6, 0]  c = heapq.nlargest(1, a + b)[0] d = heapq.nsmallest(1, a + b)[0]  print(c,d) 

Output
9 0 

Explanation: heapq.nlargest(1, a + b)[0] find the maximum and heapq.nsmallest(1, a + b)[0] to find the minimum from the merged lists efficiently, returning the largest and smallest values directly.

Using itertools.chain() with max() and min()

itertools.chain() function is used to merge multiple iterables into a single sequence without explicitly creating a new list. This allows max() and min() to operate over both lists efficiently, reducing memory overhead compared to list concatenation. Example:

Python
from itertools import chain  a = [3, 5, 7, 2, 8] b = [4, 9, 1, 6, 0]  c = max(chain(a,b)) d = min(chain(a,b))  print(c,d) 

Output
9 0 

Explanation: itertools.chain(a, b) merge both lists without creating a new list, then applies max() and min() to find the overall maximum and minimum values efficiently.

Using list merging and sorting

This method first merges both lists using the + operator and then sorts the combined list. The last element of the sorted list gives the maximum value, while the first element gives the minimum. Although simple, this method is less efficient due to the sorting step. Example:

Python
a = [3, 5, 7, 2, 8] b = [4, 9, 1, 6, 0]  c = sorted(a + b) # merge list d = c[-1] # max value e = c[0] # min value   print(d,e) 

Output
9 0 

Explanation: This code merges both lists using a + b, sorts the combined list with sorted(), then retrieves the maximum value from the last index and the minimum value from the first index.



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

Similar Reads

  • 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
  • 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 - 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 - 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 Program for Maximum and Minimum in a square matrix.
    Given a square matrix of order n*n, find the maximum and minimum from the matrix given. Examples: Input : arr[][] = {5, 4, 9, 2, 0, 6, 3, 1, 8}; Output : Maximum = 9, Minimum = 0 Input : arr[][] = {-5, 3, 2, 4}; Output : Maximum = 4, Minimum = -5 Naive Method : We find maximum and minimum of matrix
    3 min read
  • Python - Get maximum of Nth column from tuple list
    Sometimes, while working with Python lists, we can have a task in which we need to work with tuple list and get the maximum of its Nth index. This problem has application in web development domain while working with data information. Let’s discuss certain ways in which this task can be performed. Me
    7 min read
  • Python - Maximum column values in mixed length 2D List
    The usual list of list, unlike conventional C type Matrix, can allow the nested list of lists with variable lengths, and when we require the maximizations of its columns, the uneven length of rows may lead to some elements in that elements to be absent and if not handled correctly, may throw an exce
    6 min read
  • Position of maximum and minimum element in a list - Python
    In Python, lists are one of the most common data structures we use to store multiple items. Sometimes we need to find the position or index of the maximum and minimum values in the list. For example, consider the list li = [3, 5, 7, 2, 8, 1]. The maximum element is 8, and its index is 4.The minimum
    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
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