MonkeyDLuffy
MonkeyDLuffy

Reputation: 558

Getting a list of lists for all elements in list of lists greater than a certain threshold

I have a list of lists

l = [[1,2,3],[4,5,6],[7,8,9,10],[11,12,13,14,15]]

Now I want to make a new list of lists containing elements greater than 5. It should look like

k=[[],[6],[7,8,9,10],[11,12,13,14,15]]

I found a similar question here but it returns a list like j=[6, 7, 8, 9, 10, 11, 12, 13, 14, 15]. How to get a list that looks like k from l?

Upvotes: 0

Views: 471

Answers (2)

Rahul
Rahul

Reputation: 11520

You can definately use List Comp here.

k = [[i for i in subl if i > 5] for subl in l]

Upvotes: 3

user459872
user459872

Reputation: 24562

You can use filter function along with list comprehension.

>>> l = [[1,2,3],[4,5,6],[7,8,9,10],[11,12,13,14,15]]
>>> [list(filter(lambda x: x > 5, i)) for i in l]
[[], [6], [7, 8, 9, 10], [11, 12, 13, 14, 15]]

Upvotes: 0

Related Questions