Open In App
Related Articles

Python | Check element for range occurrence

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Sometimes, while working with data, we can have a simple problem in which we have ranges in form of tuple, and we need to check if a specific number lies between any of ranges suggested by tuple. This has it’s application in competitive programming. Let’s discuss certain ways in which this task can be performed. 

Method #1 : Using loop + enumerate() This task can be performed using combination of above functions. In this, we just need to iterate through each element of list and return the index of tuple pair between which the element exists using enumerate(). 

Python3

# Python3 code to demonstrate working of
# Check element for range occurrence
# Using loop + enumerate()
 
# Initializing list
test_list = [(45, 90), (100, 147), (150, 200)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Initializing element
N = 124
 
# Check element for range occurrence
# Using loop + enumerate()
res = None
for idx in (idx for idx, (sec, fir) in enumerate(test_list) if sec <= N <= fir):
    res = idx
     
# printing result
print("The index of tuple between which element occurs : " + str(res))

                    
Output : 
The original list is : [(45, 90), (100, 147), (150, 200)]
The index of tuple between which element occurs : 1

Time Complexity: O(n*n), where n is the length of the list test_list 
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list 

  Method #2 : Using next() + enumerate() + generator expression This task can also be performed using combination of above functions. In this, we just iterate through using next(). Rest everything is performed similar to the above function. 

Python3

# Python3 code to demonstrate working of
# Check element for range occurrence
# Using next() + enumerate() + generator expression
 
# Initializing list
test_list = [(45, 90), (100, 147), (150, 200)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Initializing element
N = 124
 
# Check element for range occurrence
# Using next() + enumerate() + generator expression
res = next((idx for idx, (sec, fir) in enumerate(test_list) if sec <= N <= fir), None)
     
# printing result
print("The index of tuple between which element occurs : " + str(res))

                    
Output : 
The original list is : [(45, 90), (100, 147), (150, 200)]
The index of tuple between which element occurs : 1

 Filter Function:

Approach:

In this approach, we use the filter function to create a list of tuples where the element is present in the range of each tuple. We then return the index of the first tuple in this list.

Define the function check_range_filter(lst, element) which takes a list of tuples lst and an element element as input.
Use the filter function to create a new list of tuples filtered_lst where the element is present in the range of each tuple in lst.
Check if filtered_lst is empty. If it is, return None.
Otherwise, return the index of the first tuple in lst that matches the first tuple in filtered_lst.
Use the index method of the list lst to find the index of the first tuple in filtered_lst.
Return the index found in the previous step.

Python3

def check_range_filter(lst, element):
    filtered_lst = list(filter(lambda x: x[0] <= element <= x[1], lst))
    return lst.index(filtered_lst[0]) if filtered_lst else None
 
# Example Usage
lst = [(45, 90), (100, 147), (150, 200)]
element = 110
print(check_range_filter(lst, element))  # Output: 1

                    

Output
1

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

METHOD 4:Linear Search

APPROACH:

This approach takes a list of tuples ‘lst’ and an ‘element’ as input. It searches for the tuple which has a range containing the given ‘element’. If such a tuple exists, it returns the index of the tuple. If not, it returns None. The function uses a linear search algorithm to traverse the list of tuples and find the index of the tuple containing the ‘element’.

ALGORITHM:

1.Initialize a variable ‘index’ to None
2.Traverse the list of tuples using a for loop
3.For each tuple, check if the given element lies between the range of the tuple.
4.If the element lies between the range of the tuple, set the ‘index’ variable to the index of the tuple and break out of the loop.
5.If the element is not found in any of the tuples, return None.

Python3

def check_element_range(lst, element):
    index = None
    for i in range(len(lst)):
        if lst[i][0] <= element <= lst[i][1]:
            index = i
            break
    return index
 
# Example Usage
lst = [(45, 90), (100, 147), (150, 200)]
element = 120
print(check_element_range(lst, element))  # Output: 1

                    

Output
1

Time Complexity: O(n) where n is the length of the list of tuples
Auxiliary Space: O(1)



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