Friday, December 27, 2024
Google search engine
HomeLanguagesPython | Maximum and Minimum value from two lists

Python | Maximum and Minimum value from two lists

The problem of finding maximum and minimum values in a list is quite common. But sometimes this problem can be extended in two lists and hence becomes a modified problem. This article discusses shorthands by which this task can be performed easily. Let’s discuss certain ways in which this problem can be solved. 

Method #1: Using max() + min() + “+” operator The maximum and minimum values can be determined by the conventional max and min function of python and the extension of one to two lists can be dealt using the “+” operator. 

Python3




# Python3 code to demonstrate
# maximum and minimum values in two lists
# using max() + min() + "+" operator
 
# initializing lists
test_list1 = [1, 3, 4, 5, 2, 6]
test_list2 = [3, 4, 8, 3, 10, 1]
 
# printing the original lists
print ("The original list 1 is : " + str(test_list1))
print ("The original list 2 is : " + str(test_list2))
 
# using max() + min() + "+" operator
# maximum and minimum values in two lists
max_all = max(test_list1 + test_list2)
min_all = min(test_list1 + test_list2)
 
# printing result
print ("The maximum of both lists is : " + str(max_all))
print ("The minimum of both lists is : " + str(min_all))


Output:

The original list 1 is : [1, 3, 4, 5, 2, 6]
The original list 2 is : [3, 4, 8, 3, 10, 1]
The maximum of both lists is : 10
The minimum of both lists is : 1

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

Method #2: Using max() + min() + chain() Another method to perform this particular task is by using the chain function which performs the task similar to the “+” operator but using an iterator, hence faster. 

Python3




# Python3 code to demonstrate
# maximum and minimum values in two lists
# using max() + min() + "+" operator
from itertools import chain
 
# initializing lists
test_list1 = [1, 3, 4, 5, 2, 6]
test_list2 = [3, 4, 8, 3, 10, 1]
 
# printing the original lists
print ("The original list 1 is : " + str(test_list1))
print ("The original list 2 is : " + str(test_list2))
 
# using max() + min() + "+" operator
# maximum and minimum values in two lists
max_all = max(chain(test_list1, test_list2))
min_all = min(chain(test_list1, test_list2))
 
# printing result
print ("The maximum of both lists is : " + str(max_all))
print ("The minimum of both lists is : " + str(min_all))


Output:

The original list 1 is : [1, 3, 4, 5, 2, 6]
The original list 2 is : [3, 4, 8, 3, 10, 1]
The maximum of both lists is : 10
The minimum of both lists is : 1

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

Method #3: Using list comprehension

Python3




list1 = [1, 3, 4, 5, 2, 6];list2 = [3, 4, 8, 3, 10, 1]
x=[i for i in list1+list2]
print("maximum element is ",max(x),"minimum element is ",min(x))


Output

maximum element is  10 minimum element is  1

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

Method #4: Using enumerate function

Python3




list1 = ['1', '3', '4', '5', '2', '6'];list2 = ['3', '4', '8', '3', '10', '1']
x=[int(i) for a,i in enumerate(list1+list2)]
print("maximum element is ",max(x),"minimum element is ",min(x))


Output

maximum element is  10 minimum element is  1

Time Complexity: O(n), where n is the length of the list test_list 
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list 

Method #5: Using extend

Another approach to find the maximum and minimum value from two lists is to use the max() and min() functions on the combined list. This can be done using the extend() method of lists.

Python3




list1 = [1, 3, 4, 5, 2, 6]
list2 = [3, 4, 8, 3, 10, 1]
 
# combine the lists
list1.extend(list2)
 
# find the maximum and minimum value
max_value = max(list1)
min_value = min(list1)
 
print("Maximum value:", max_value)
print("Minimum value:", min_value)
#This code is contributed by Edula Vinay Kumar Reddy


Output

Maximum value: 10
Minimum value: 1

This approach has a time complexity of O(n), where n is the total number of elements in both lists. 
It has an Auxiliary Space of O(n) as well since a new list is created to store the combined list.

Method #6: Using heapq module

Use the heapq module in Python to get the n largest and n smallest elements from a list efficiently. We can use the heapq.nlargest() function to get the n largest elements and heapq.nsmallest() function to get the n smallest elements. Here, we can use n as 1 to get the maximum and minimum values from the two lists.

Step-by-step approach:

  • Import the heapq module.
  • Initialize two lists.
  • Concatenate the two lists using the ‘+’ operator and store it in a variable.
  • Find the maximum element by using heapq.nlargest(1, combined_list) and store it in a variable.
  • Find the minimum element by using heapq.nsmallest(1, combined_list) and store it in a variable.
  • Print the maximum and minimum values.

Python3




# Python3 code to demonstrate
# maximum and minimum values in two lists
# using heapq.nlargest() and heapq.nsmallest()
  
# import the heapq module
import heapq
  
# initializing lists
test_list1 = [1, 3, 4, 5, 2, 6]
test_list2 = [3, 4, 8, 3, 10, 1]
  
# concatenating the two lists using '+' operator
combined_list = test_list1 + test_list2
  
# finding the maximum element
max_all = heapq.nlargest(1, combined_list)
  
# finding the minimum element
min_all = heapq.nsmallest(1, combined_list)
  
# printing the results
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
print("The maximum of both lists is : " + str(max_all[0]))
print("The minimum of both lists is : " + str(min_all[0]))


Output

The original list 1 is : [1, 3, 4, 5, 2, 6]
The original list 2 is : [3, 4, 8, 3, 10, 1]
The maximum of both lists is : 10
The minimum of both lists is : 1

Time Complexity: O(nlogn), where n is the length of the combined list. The heapq module uses a heap data structure, which has a logarithmic time complexity for push and pop operations.
Auxiliary Space: O(1), as we are not using any extra space for storing the elements. We are only storing the maximum and minimum values in variables.

Method #7: Using a for loop to find the maximum and minimum values

  • Initialize the two lists
  • Initialize a variable max_all to the minimum possible value and a variable min_all to the maximum possible value
  • Use a for loop to iterate over each element of both lists, and update max_all and min_all accordingly.
  • Print the results

Python3




# initializing lists
test_list1 = [1, 3, 4, 5, 2, 6]
test_list2 = [3, 4, 8, 3, 10, 1]
 
# finding the maximum and minimum elements
max_all = float('-inf')
min_all = float('inf')
for num in test_list1 + test_list2:
    if num > max_all:
        max_all = num
    if num < min_all:
        min_all = num
 
# printing the results
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
print("The maximum of both lists is : " + str(max_all))
print("The minimum of both lists is : " + str(min_all))


Output

The original list 1 is : [1, 3, 4, 5, 2, 6]
The original list 2 is : [3, 4, 8, 3, 10, 1]
The maximum of both lists is : 10
The minimum of both lists is : 1

Time complexity: O(n) (since we are only iterating over the elements once)
Auxiliary space: O(1) (since we are not using any extra data structures)

RELATED ARTICLES

Most Popular

Recent Comments