Python – Test if List contains elements in Range
Last Updated :
10 May, 2023
A lot of times, while working with data, we have a problem in which we need to make sure that a container or a list is having elements in just one range. This has application in Data Domains. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using loop This is brute force method in which this task can be performed. In this, we just check using if condition if element falls in range, and break if we find even one occurrence out of range.
Python3
test_list = [ 4 , 5 , 6 , 7 , 3 , 9 ]
print ("The original list is : " + str (test_list))
i, j = 3 , 10
res = True
for ele in test_list:
if ele < i or ele > = j :
res = False
break
print ("Does list contain all elements in range : " + str (res))
|
Output :
The original list is : [4, 5, 6, 7, 3, 9]
Does list contain all elements in range : True
Time Complexity: O(n)
Auxiliary Space: O(1)
Method #2 : Using all() This is alternative and shorter way to perform this task. In this we use check operation as a parameter to all() and returns True when all elements in range.
Python3
test_list = [ 4 , 5 , 6 , 7 , 3 , 9 ]
print ("The original list is : " + str (test_list))
i, j = 3 , 10
res = all (ele > = i and ele < j for ele in test_list)
print ("Does list contain all elements in range : " + str (res))
|
Output :
The original list is : [4, 5, 6, 7, 3, 9]
Does list contain all elements in range : True
Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(1), as the code uses a constant amount of additional space regardless of the size of the input.
Method #3 : Using list comprehension and len()
This method uses a simple list comprehension which returns all elements which fall out of the given range. And then the length of this list is taken, if it is 0 that means all elements are in range and returns True, else returns False.
Python3
test_list = [ 4 , 5 , 6 , 7 , 3 , 9 ]
print ( "The original list is : " + str (test_list))
i, j = 3 , 10
out_of_range = len ([ele for ele in test_list if ele < i or ele > = j]) = = 0
print ( "Does list contain all elements in range : " + str (out_of_range))
|
Output
The original list is : [4, 5, 6, 7, 3, 9]
Does list contain all elements in range : True
Time complexity: O(n)
Auxiliary Space: O(n)
Method 4: Use the built-in set() function
Step-by-step approach:
- Initialize a list test_list containing some elements.
- Initialize two variables i and j to define a range.
- Check if any element in test_list is within the range defined by i and j.
- If any element is within the range, set the result res to True. Otherwise, set res to False.
- Print the result of whether any element in the list is within the range.
Python3
test_list = [ 4 , 5 , 6 , 7 , 3 , 9 ]
i, j = 3 , 10
res = any (i < = x < j for x in test_list)
print ( "Does list contain any element in range: " + str (res))
|
Output
Does list contain any element in range: True
Time complexity: O(n), where n is the length of the list.
Auxiliary space: O(1) because it only uses a few variables to store the range boundaries and the result.
Method 5: Using filter() function
Step-by-step approach:
- Define a function that takes an integer x and returns True if it lies within the given range, i.e., i <= x < j.
- Use the filter() function to filter out the elements of the list that satisfy the above condition.
- Check if the filtered list is not empty using the bool() function.
- Assign the result to a variable ‘res’.
- Print the final result.
Python3
test_list = [ 4 , 5 , 6 , 7 , 3 , 9 ]
i, j = 3 , 10
def in_range(x):
return i < = x < j
filtered_list = list ( filter (in_range, test_list))
res = bool (filtered_list)
print ( "Does list contain any element in range: " + str (res))
|
Output
Does list contain any element in range: True
Time complexity: O(n)
Auxiliary space: O(n) (to store the filtered list)
Please Login to comment...