Reputation: 558
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
Reputation: 11520
You can definately use List Comp here.
k = [[i for i in subl if i > 5] for subl in l]
Upvotes: 3
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