Skip to content
Related Articles
Open in App
Not now

Related Articles

Python | Remove all sublists outside the given range

Improve Article
Save Article
  • Last Updated : 30 Dec, 2022
Improve Article
Save Article

Given, list of lists and a range, the task is to traverse each sub-list and remove sublists containing elements falling out of given range. Examples:

Input : 
left= 10, right = 17, list = [[0, 1.2, 3.4, 18.1, 10.1],
                              [10.3, 12.4, 15, 17, 16, 11],
                              [100, 10, 9.2, 11, 13, 17.1], ]

Output: [[10.3, 12.4, 15, 17, 16, 11]]


Input : 
left= 1, right = 9, list = [[11, 12, 15, 17, 3],
                            [3, 1, 4, 5.2, 9, 19],
                            [2, 4, 6, 7.2, 8.9]]

Output: [[2, 4, 6, 7.2, 8.9]]

  Method #1 : Iterating through each sublist. 

Python3




# Python code to remove all the
# sublist outside the given range
 
# Initialisation of list of list
list = [[0,1.2,3.4,18.1,10.1],
        [10.3,12.4,15,17,16,11],
        [1000,100,10,3.2,11,13, 17.1], ]
 
# Defining range
left, right = 10, 17
 
# initialization of index
b=0
 
for t in list:
    a=0
    for k in t:
        if k<left or k>right:
            a=1
    if a==1:
        list.pop(b)
    b=b+1
 
# printing output
print(list)

Output:

[[10.3, 12.4, 15, 17, 16, 11]]

  Method #2: Using list comprehension 

Python3




# Python code to remove all the
# sublist outside the given range
 
# Initialisation of list of list
list = [[11, 12, 15, 17, 3],
        [3, 1, 4, 5.2, 9, 19],
        [2, 4, 6, 7.2, 8.9]]
 
# Defining range
left = 1
right = 9
 
# Using list comprehension
Output = [i for i in list if (min(i)>=left and max(i)<=right)]
 
# Printing output
print (Output)

Output:

[[2, 4, 6, 7.2, 8.9]]

 Method #3: One additional approach that can be used to remove all sublists outside a given range is to use a filter function along with a custom function that checks if any elements in the sublist fall outside the given range.

Here is an example of how to use a filter function and a custom function to remove all sublists outside a given range:

Python3




# Initialisation of list of lists
lst = [[0, 1.2, 3.4, 18.1, 10.1],
       [10.3, 12.4, 15, 17, 16, 11],
       [100, 10, 9.2, 11, 13, 17.1]]
 
# Define the range
left, right = 10, 17
 
# Define the custom function that checks if any elements in the sublist fall outside the given range
def check_range(lst, left, right):
    for i in lst:
        if i < left or i > right:
            return False
    return True
 
# Use the filter function to remove all sublists outside the given range
filtered_lst = list(filter(lambda x: check_range(x, left, right), lst))
 
# Print the filtered list
print(filtered_lst)
#This code is contributed by Edula Vinay Kumar Reddy

Output

[[10.3, 12.4, 15, 17, 16, 11]]

The filter function has a time complexity of O(n), where n is the number of elements in the input list, because it iterates over all the elements in the list. The custom function has a time complexity of O(m), where m is the number of elements in the sublist, because it iterates over all the elements in the sublist. The overall time complexity of this approach is O(nm), because both the filter function and the custom function have a linear time complexity. The space complexity of this approach is also O(nm), because the output is a new list that is the same size as the input list.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!