The Wayback Machine - https://web.archive.org/web/20240717024651/https://www.geeksforgeeks.org/python-remove-all-sublists-outside-the-given-range/
Open In App

Python | Remove all sublists outside the given range

Last Updated : 17 Apr, 2023
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
Report

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]]

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

  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]]

Time Complexity: O(n*n), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(1), constant space needed 

 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.



Similar Reads

Print all sublists of a list in Python
Given a list, print all the sublists of a list. Python provides efficient and elegant ways to generate and manipulate sublists. In this article, we will explore different methods how to print all sublists of a given list. Examples: Input: list = [1, 2, 3] Output: [[], [1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]Input: [1, 2, 3, 4] Output: [[], [1], [1
3 min read
How To Place Legend Outside the Plot with Seaborn in Python?
Seaborn is a Python data visualization library based on matplotlib. It provides a high-level interface for drawing attractive and informative statistical graphics. Basically, it helps us stylize our basic plot made using matplotlib. Moreover, it also provides us different plotting techniques to ease our Exploratory Data Analysis(EDA). With these pl
2 min read
Python - Import module outside directory
Modules are simply a python .py file from which we can use functions, classes, variables in another file. To use these things in another file we need to first import that module into that file. If the module exists in the same directory as the file, we can directly import it using the syntax import module_name. But if it exists in a different direc
4 min read
How to Place Legend Outside of the Plot in Matplotlib?
In this article, we will see how to put the legend outside the plot. Let's discuss some concepts : Matplotlib: Matplotlib is an amazing visualization library in Python for 2D plots of arrays. Matplotlib is a multi-platform data visualization library built on NumPy arrays and designed to work with the broader SciPy stack. It was introduced by John H
3 min read
Python | Remove all duplicates words from a given sentence
Given a sentence containing n words/strings. Remove all duplicates words/strings which are similar to each others. Examples: Input : Geeks for Geeks Output : Geeks for Input : Python is great and Java is also great Output : is also Java Python and great We can solve this problem quickly using python Counter() method. Approach is very simple. 1) Spl
7 min read
Remove All Duplicates from a Given String in Python
We are given a string and we need to remove all duplicates from it. What will be the output if the order of character matters? Examples: Input : geeksforgeeks Output : geksfor This problem has an existing solution please refer to Remove all duplicates from a given string. Method 1: [GFGTABS] Python from collections import OrderedDict # Function to
3 min read
Python groupby method to remove all consecutive duplicates
Given a string S, remove all the consecutive duplicates. Examples: Input : aaaaabbbbbb Output : ab Input : geeksforgeeks Output : geksforgeks Input : aabccba Output : abcba We have existing solution for this problem please refer Remove all consecutive duplicates from the string link. We can solve this problem in python quickly using itertools.group
2 min read
Python | Remove all values from a list present in other list
Sometimes we need to perform the operation of removing all the items from the lists that are present in another list, i.e we are given some of the invalid numbers in one list which need to be get ridden from the original list. Let's discuss various ways How to remove the elements of a list from another list in Python. Illustration: Input: List one
10 min read
How to remove all decimals from a number using Python?
In python programming, sometimes it is necessary to remove all decimals from a number to get the required output. These decimals are also called Floating point numbers in Python. Basically, there are 3 numerical data types in python. They are integers(int()), floating-point numbers(float()) and complex(complex()) data types. Type conversion in pyth
2 min read
Remove all Alphanumeric Elements from the List - Python
Alphanumeric elements consist of only alphabetical and numerical characters. Any of the special characters are not included in alphanumeric elements. This article is going to show you multiple ways to remove all the alphanumeric elements from a List of strings using Python. Example 1: Input: ['a','b', '!', '1', '2', '@', 'abc@xyz.com'] Output: ['!'
2 min read
Practice Tags :
three90RightbarBannerImg