Finding the Maximum and Minimum value from two Python Lists


Introduction

Python, now widely regarded as one of the most versatile programming languages, offers a plethora of built−in functions and methods that simplify complex tasks. In this article, we dive into finding the maximum and minimum values among two Python lists rapidly using these native capabilities. Whether we are a beginner or an experienced programmer, understanding how to leverage these functionalities can significantly streamline our code.

Finding the Maximum and Minimum values from two Python lists

Before we explore how to find max and min values from two separate lists, let's first familiarize ourselves with the respective functions provided by Python. This approach works great for smaller datasets−however; It may not be optimal while working with much more extensive dataset sizes. For that purpose, another advanced algorithmic approach may work well and need deeper explorations.

Syntax

  • 'max()' − This function returns the highest value within an iterable for example list, or if multiple arguments are supplied, it returns the largest argument.

  • 'min()' − Conversely, this function returns the lowest value within an iterable or amongst multiple arguments provided.

Then moving on to how to identify maximum and minimum values across different approaches involving two distinct lists are,

Approach

Approach 1: Using the min and max function

Approach 2: Using the zip function

Approach 3: Using the lambda function

Approach 1: Python Program to find Maximum and Minimum Value from two Python lists using min and max function

Consider having two separate lists List A and List B, which contain integer elements. To determine their maximum and minimum values effortlessly, we can use the predefined functions.

Algorithm

Step 1 :In this case, by merging both lists through concatenation that is using the operator '+' to create Combined_list.

Step 2 :Every element is considered while calculating both maximum and minimum values using `max()’ and `min()`, respectively.

Step 3 :The output is printed finally with the values.

Example

#Initializing the two lists with integer data types
List_A = [5, 9, 13]
List_B = [7,-3 ,11]
#using the concatenation operator that is “+”, we can add the two lists
Combined_list = List_A + List_B
#with the functions, the user can get the small and high values
maximum_value = max(Combined_list)
minimum_value = min(Combined_list)
#Output is printed using print function
print("Maximum Value:", maximum_value)
print("Minimum Value:", minimum_value)

Output

Maximum Value: 13
Minimum Value: -3

Approach 2: To find the maximum and minimum value from two Python lists using the zip function

If we want to extract corresponding maximum and minimum values between two related lists we can use the zip function. This approach is exceptionally useful for applications that demand data comparison, such as tracking stock prices or analyzing temperature variations across different locations.

Algorithm

Step 1 :By utilizing the built-in `zip()` function to pair corresponding elements and employ list comprehension along with `max()` and `min()`.

Step 2 :This ensures the efficient extraction of both maximum and minimum values from the combined lists.

Step 3 :With the help of a zip function with two functions, the values are calculated and then returned using the print statement.

Example

#creating two lists with integer data types
List_X = [2, 8, 10]
List_Y = [5, -4 ,12]
#function is defined with two parameters as inputs to get the minimum and maximum values
maximum_values = [max(x,y) for x,y in zip(List_X,List_Y)]
minimum_values = [min(x,y) for x,y in zip(List_X,List_Y)]

#printing the output values
print("Maximum Values:", maximum_values)
print("Minimum Values:", minimum_values)

Output

Maximum Values: [5, 8, 12]
Minimum Values: [2, -4, 10]

Approach 3: Python program to get the Maximum and Minimum value from two lists using the lambda function

At times we might want to specify additional conditions when searching for max or min values. Python offers a helpful feature where we can apply predicates using lambda functions within these built−in methods.

Algorithm

Step 1 :Combining both lists through concatenation allows the user to single out only positive numbers by applying a filter through the lambda function.

Step 2 :The resulting filtered collection is then passed into `min()` to obtain the desired smallest value efficiently.

Step 3 :The ‘max()’ function is used to get the highest value of the given two−list data structure.

Step 4 :The output is returned.

Example

#list data structure is initialized with both positive and negative numbers
list_p = [9,6,1]
list_q = [2,11 ,-15]
#lambda is always declared along with the filter function
#filter method
filtered_list = list(filter(lambda a: (a > 0), list_p + list_q))
#Defining the required function to get the small and high value
minimum_value = min(filtered_list)
maximum_value = max(filtered_list)
#print function will print the output
print("Lowest value:", minimum_value)
print("Highest value:", maximum_value) 

Output

Lowest value: 1
Highest value: 11

Conclusion

Python's extensive set of built−in functionalities simplifies complex operations like finding maximum and minimum values within multiple lists dramatically. By leveraging techniques explained in various approaches above like whether independent lists or comparing corresponding elements. The functions used are the lambda, filter, max, and min methods.

Updated on: 07-Aug-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements