Maximum and Minimum value from two lists Python (original) (raw)
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.
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)
`
**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.