Open In App

Python – Test if elements of list are in Min/Max range from other list

Improve
Improve
Like Article
Like
Save
Share
Report

Given two lists, the task is to write a Python Program to return true if all elements from second list are in range of min and max values of the first list.

Examples:

Input : test_list = [5, 6, 3, 7, 8, 10, 9], range_list = [4, 7, 9, 6] 
Output : True 
Explanation : Min and max in list 1 are 3 and 10, all elements are in range in other list.

Input : test_list = [5, 6, 3, 7, 8, 10, 9], range_list = [4, 7, 9, 16] 
Output : False 
Explanation : Min and max in list 1 are 3 and 10, all elements are not in range in other list. 

Method #1 : Using loop + min() + max()

In this, we iterate for all the elements in second list and compare for each element, if any element is less than min or greater than max, result is flagged off and false is returned.

Python3




# Python3 code to demonstrate working of
# Min/Max range test from other list
# Using loop + min() + max()
 
# initializing list
test_list = [5, 6, 3, 7, 8, 10, 9]
 
# printing original lists
print("The original list is : " + str(test_list))
 
# initializing range_list
range_list = [4, 7, 9, 6]
 
res = True
for ele in range_list:
 
    # flag off list in case of any off range element
    if ele  max(test_list):
        res = False
        break
 
# printing result
print("Are all elements in min/max range? : " + str(res))


Output

The original list is : [5, 6, 3, 7, 8, 10, 9]
Are all elements in min/max range? : True

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

Method #2 : Using all() + min() + max()

In this, we check for all the elements to be in range using all(), and min() and max() are used to get the maximum and minimum elements. 

Python3




# Python3 code to demonstrate working of
# Min/Max range test from other list
# Using all() + min() + max()
 
# initializing list
test_list = [5, 6, 3, 7, 8, 10, 9]
 
# printing original lists
print("The original list is : " + str(test_list))
 
# initializing range_list
range_list = [4, 7, 9, 6]
 
# checking for all values in range using all()
res = all(ele >= min(test_list) and ele <= max(test_list)
          for ele in range_list)
 
# printing result
print("Are all elements in min/max range? : " + str(res))


Output

The original list is : [5, 6, 3, 7, 8, 10, 9]
Are all elements in min/max range? : True

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

Method #3: Using set intersection

  • Initialize the list named “test_list” with values [5, 6, 3, 7, 8, 10, 9].
  • Print the original list using the “print()” function with a string message.
  • Initialize another list named “range_list” with values [4, 7, 9, 6].
  • Use the “set()” function to create two sets from the “test_list” and “range_list”.
  • Use the “intersection()” method to find the common elements between the two sets.
  • Assign the resulting set of common elements to a variable named “common_elements”.
  • Use a generator expression with the “all()” function to check if all the elements in the “common_elements” set are within the minimum and maximum range of the “test_list”.
  • Assign the resulting boolean value to a variable named “res”.
  • Print the result using the “print()” function with a string message.

Python3




# Python3 code to demonstrate working of
# Min/Max range test from other list
# Using set intersection
 
# initializing list
test_list = [5, 6, 3, 7, 8, 10, 9]
 
# printing original lists
print("The original list is : " + str(test_list))
 
# initializing range_list
range_list = [4, 7, 9, 6]
 
# using set intersection to find common elements between test_list and range_list
common_elements = set(test_list).intersection(set(range_list))
 
# checking if all common elements are within the range
res = all(ele >= min(test_list) and ele <= max(test_list)
          for ele in common_elements)
 
# printing result
print("Are all elements in min/max range? : " + str(res))


Output

The original list is : [5, 6, 3, 7, 8, 10, 9]
Are all elements in min/max range? : True

Time complexity: O(n), where n is the length of the longer list between test_list and range_list.
Auxiliary space: O(m), where m is the length of the shorter list between test_list and range_list, as a set of the shorter list is created.

Method #4: Use list comprehension

  • Initialize the original list test_list with the values [5, 6, 3, 7, 8, 10, 9].
  • Print the original list using the print() function and a string concatenation to display the message “The original list is : ” followed by the list converted to a string using the str() function: print(“The original list is : ” + str(test_list)).
  • Initialize the range list range_list with the values [4, 7, 9, 6].
  • Create a new filtered list filtered_list using a list comprehension that iterates through the values of range_list and checks if each value is between the minimum and maximum values of test_list using the min() and max() functions: [x for x in range_list if min(test_list) <= x <= max(test_list)].
  • Compare the filtered list filtered_list with the original range list range_list using the == operator and assign the result to the variable res: res = filtered_list == range_list.
  • Print the result using the print() function and a string concatenation to display the message “Are all elements in min/max range? : ” followed by the result converted to a string using the str() function: print(“Are all elements in min/max range? : ” + str(res)).

Python3




# Python3 code to demonstrate working of
# Min/Max range test from other list
# Using list comprehension
 
# initializing list
test_list = [5, 6, 3, 7, 8, 10, 9]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing range list
range_list = [4, 7, 9, 6]
 
# filtering range list to remove elements outside of min/max range
filtered_list = [x for x in range_list if min(test_list) <= x <= max(test_list)]
 
# checking if filtered list is the same as range list
res = filtered_list == range_list
 
# printing result
print("Are all elements in min/max range? : " + str(res))


Output

The original list is : [5, 6, 3, 7, 8, 10, 9]
Are all elements in min/max range? : True

Time complexity: O(n), where n is the length of the original list, since we need to iterate through the entire list to find the minimum and maximum values. 
Auxiliary space: O(n), since we create a new filtered list that could potentially contain all n elements of the original range list.



Last Updated : 10 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads