How to remove sublists whose length is outside a range in a list of lists with Python

1 Answer

0 votes
def filter_by_length(lst, min_len, max_len):
    return [
        sub for sub in lst
        if min_len <= len(sub) <= max_len
    ]


list_of_list = [[1, 2], [10, 20], [3, 4, 6], [100], [7, 8], [9, 10, 11, 12]]
min_len = 2
max_len = 3

result = filter_by_length(list_of_list, min_len, max_len)
print(result)



'''
run:

[[1, 2], [10, 20], [3, 4, 6], [7, 8]]

'''

 



answered Feb 17 by avibootz
...