'Create a new list from a list when a certain condition is met in python
I want to make a new list of lists when the value in the 3rd column is of the highest 10%.
My list of lists currently look like this:
[[1, -1, 10, 0]]
[[2, 1, 20, 5]]
[[3, 2, 15, 10], [4, 2, 85, 10], [5, 2, 90, 10]]
[[6, 3, 75, 11], [7, 4, 78, 11], [8, 5, 80, 11]]
[[9, 6, 13, 14]]
[[10, 7, 50, 17]]
[[11, 8, 70, 30], [12, 8, 95, 30], [13, 8, 90, 30]].....
I was thinking of something along the lines of:
M1 = max(row1)[2] (maximum value from the 3rd column)
if row1[i][2] >= M1*(0.1):
newrow1.append()
Any help would be greatly appreciated!! Thanks in advance
Solution 1:[1]
You can use list comprehension:
[item for item in l if item[2]>10]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
