Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Finding the Maximum and Minimum value from two Python Lists
Python provides several built-in functions to find maximum and minimum values from two lists. Understanding these approaches helps you choose the right method based on your specific requirements.
Built-in Functions
Python offers two essential functions for finding extreme values:
max() Returns the highest value from an iterable or multiple arguments
min() Returns the lowest value from an iterable or multiple arguments
Method 1: Using List Concatenation
Combine both lists and find the overall maximum and minimum values ?
# Initialize two lists with integer values
list_a = [5, 9, 13]
list_b = [7, -3, 11]
# Combine both lists using concatenation
combined_list = list_a + list_b
# Find maximum and minimum values
maximum_value = max(combined_list)
minimum_value = min(combined_list)
print("Maximum Value:", maximum_value)
print("Minimum Value:", minimum_value)
Maximum Value: 13 Minimum Value: -3
Method 2: Using zip() for Element-wise Comparison
Compare corresponding elements and find maximum/minimum at each position ?
# Create two lists with integer values
list_x = [2, 8, 10]
list_y = [5, -4, 12]
# Find element-wise maximum and minimum 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)]
print("Maximum Values:", maximum_values)
print("Minimum Values:", minimum_values)
Maximum Values: [5, 8, 12] Minimum Values: [2, -4, 10]
Method 3: Using Lambda with Filter
Apply conditions using lambda functions before finding extreme values ?
# Initialize lists with positive and negative numbers
list_p = [9, 6, 1]
list_q = [2, 11, -15]
# Filter only positive numbers using lambda
filtered_list = list(filter(lambda x: x > 0, list_p + list_q))
# Find minimum and maximum from filtered values
minimum_value = min(filtered_list)
maximum_value = max(filtered_list)
print("Lowest positive value:", minimum_value)
print("Highest positive value:", maximum_value)
Lowest positive value: 1 Highest positive value: 11
Comparison
| Method | Use Case | Output Type |
|---|---|---|
| List Concatenation | Overall max/min from both lists | Single values |
| zip() Function | Element-wise comparison | List of values |
| Lambda with Filter | Conditional filtering | Single values |
Conclusion
Use list concatenation for overall extremes, zip() for element-wise comparisons, and lambda with filter for conditional operations. Choose the method that best fits your specific data analysis needs.
