Maximum and Minimum value from two lists - Python
Last Updated :
07 Apr, 2025
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)
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.
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)
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.
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)
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)
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.
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 - 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 e
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