Python - Test if elements of list are in Min/Max range from other list
Last Updated :
10 May, 2023
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))
OutputThe 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))
OutputThe 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))
OutputThe 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))
OutputThe 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.
Similar Reads
Python program to test if all elements in list are maximum of K apart
Given a list of numbers, the task is to write a Python program to test if all elements are maximum of K apart. Examples: Input : test_list = [475, 503, 425, 520, 470, 500], K = 100Output : True Explanation : Maximum element is 520 and minimum is 425, 520-425 = 95, which is less than 100, hence eleme
5 min read
Test if all elements are present in list-Python
The task of testing if all elements are present in a list in Python involves checking whether every item in a target list exists within a reference list. For example, given two lists a = [6, 4, 8, 9, 10] and b = [4, 6, 9], the task is to confirm that all elements in list b are also found in list a.U
3 min read
Python - Maximum of K element in other list
Given two lists, extract maximum of elements with similar K in corresponding list. Input : test_list1 = [4, 3, 6, 2, 8], test_list2 = [3, 6, 3, 4, 3], K = 3 Output : 8 Explanation : Elements corresponding to 3 are, 4, 6, and 8, Max. is 8. Input : test_list1 = [10, 3, 6, 2, 8], test_list2 = [5, 6, 5,
4 min read
Python - Test if all elements in list are of same type
When working with lists in Python, there are times when we need to ensure that all the elements in the list are of the same type or not. This is particularly useful in tasks like data analysis where uniformity is required for calculations. In this article, we will check several methods to perform th
2 min read
Python | Get positive elements from given list of lists
Given a list of list, the task is to get only positive element from given list. Below are some ways to solve the above problem. Method #1: Using Iteration Python3 # Python code to get positive # element from list of list # List Initialisation Input = [[10, -11, 222], [42, -222, -412, 99, -87]] Outpu
5 min read
Python - Test if string contains element from list
Testing if string contains an element from list is checking whether any of the individual items in a list appear within a given string.Using any() with a generator expressionany() is the most efficient way to check if any element from the list is present in the list. Pythons = "Python is powerful an
3 min read
Python - Test for all Even elements in the List for the given Range
Given a List of elements, test if all elements are even in a range. Input : test_list = [3, 1, 4, 6, 8, 10, 1, 9], i, j = 2, 5 Output : True Explanation : 4, 6, 8, 10, all are even elements in range. Input : test_list = [3, 1, 4, 6, 87, 10, 1, 9], i, j = 2, 5 Output : False Explanation : All not eve
2 min read
Python - Ranged Maximum Element in String List
Sometimes, while working with Python data, we can have a problem in which we have data in form of String List and we require to find the maximum element in that data, but that also in a certain range of indices. This is quite peculiar problem but can have application in data domains. Let's discuss c
4 min read
Python - Extract records if Kth elements not in List
Given list of tuples, task is to extract all the tuples where Kth index elements are not present in argument list. Input : test_list = [(5, 3), (7, 4), (1, 3), (7, 8), (0, 6)], arg_list = [6, 8, 8], K = 1 Output : [(5, 3), (7, 4), (1, 3)] Explanation : All the elements which have either 6 or 8 at 1s
4 min read
Python | Check if element exists in list of lists
Given a list of lists, the task is to determine whether the given element exists in any sublist or not. Given below are a few methods to solve the given task. Method #1: Using any() any() method return true whenever a particular element is present in a given iterator. Python3 # Python code to demons
5 min read