Python remove empty elements from list of lists
Your attempt will remove empty lists from the list of lists, not empty elements from the sublists. Instead, apply the filter to the sublists:
str_list = [list(filter(None, lst)) for lst in list_of_lists]
The call to filter()
is wrapped with list()
in case you happen to try this in python3 later, as filter()
returns an iterator in py3.
Note that since you tagged this as csv you might have to be careful as filtering might produce rows with differing lengths and items in wrong columns. If you know that for each row the 2 last items will always be empty, you could slice them out:
str_list = [row[:-2] for row in list_of_lists]
R: removing NULL elements from a list
The closest you'll be able to get is to first name the list elements and then remove the NULLs.
names(x) <- seq_along(x)
## Using some higher-order convenience functions
Filter(Negate(is.null), x)
# $`11`
# [1] 123
#
# $`13`
# [1] 456
# Or, using a slightly more standard R idiom
x[sapply(x, is.null)] <- NULL
x
# $`11`
# [1] 123
#
# $`13`
# [1] 456
Recursive remove NULL elements of list of lists
One approach is to use rrapply
in the rrapply
-package (extension of base rapply
):
library(rrapply)
x <- list(a=1, b=2, c=list(ca=1, cb=2, cc=NULL), d=NULL)
rrapply(x, condition = Negate(is.null), how = "prune")
#> $a
#> [1] 1
#>
#> $b
#> [1] 2
#>
#> $c
#> $c$ca
#> [1] 1
#>
#> $c$cb
#> [1] 2
Benchmark timings
Benchmarking the computation time of rrapply
against rlist's list.clean
function for some large nested lists, I get the following results:
## recursively create nested list with dmax layers and 50% NULL elements
f <- function(len, d, dmax) {
x <- vector(mode = "list", length = len)
for(i in seq_along(x)) {
if(d + 1 < dmax) {
x[[i]] <- Recall(len, d + 1, dmax)
} else {
x[[i]] <- list(1, NULL)
}
}
return(x)
}
## long shallow list (3 layers, total 5e5 nodes)
x_long <- f(len = 500, d = 1, dmax = 3)
microbenchmark::microbenchmark(
rlist = rlist::list.clean(x_long, recursive = TRUE),
rrapply = rrapply::rrapply(x_long, condition = Negate(is.null), how = "prune"),
check = "equal",
times = 5L
)
#> Unit: milliseconds
#> expr min lq mean median uq max neval
#> rlist 2331.4914 2343.3001 2438.9674 2441.3850 2512.3484 2566.3121 5
#> rrapply 353.7169 393.0646 400.8198 399.7971 417.7235 439.7972 5
## deeply nested list (18 layers, total 2^18 nodes)
x_deep <- f(len = 2, d = 1, dmax = 18)
microbenchmark::microbenchmark(
rlist = rlist::list.clean(x_deep, recursive = TRUE),
rrapply = rrapply::rrapply(x_deep, condition = Negate(is.null), how = "prune"),
check = "equal",
times = 5L
)
#> Unit: milliseconds
#> expr min lq mean median uq max neval
#> rlist 2167.2946 2251.5203 2279.9963 2292.5045 2332.4432 2356.2188 5
#> rrapply 268.9463 274.7437 325.9585 292.4559 354.1607 439.4857 5
Remove empty strings from a list of strings
I would use filter
:
str_list = filter(None, str_list)
str_list = filter(bool, str_list)
str_list = filter(len, str_list)
str_list = filter(lambda item: item, str_list)
Python 3 returns an iterator from filter
, so should be wrapped in a call to list()
str_list = list(filter(None, str_list))
Java - Remove null from list of Objects
Mutable list
You can use List::removeIf
with a predicate detecting null
items.
List<Object[]> mutableList = new ArrayList<>(Arrays.asList(
new Object[] {},
null,
new Object[] {}));
mutableList.removeIf(Objects::isNull);
Immutable list
In this case you have to use Stream API or a for-loop to find non-null elements and add them to a new list.
List<Object[]> immutableList = Arrays.asList(
new Object[] {},
null,
new Object[] {});
List<Object[]> newList = immutableList.stream()
.filter(Objects::nonNull)
.collect(Collectors.toList());
Remove values / nan / empty string( ) from list of lists in python
Here is the corrected version.
from math import isnan # numpy.isnan can also be used instead
# If the list contains only strings, `if e != ""` can be simplied to `if e`
a = [[e for e in l if e != ""] for l in a]
# Use isnan function to check for nan values
b = [[e for e in l if not isnan(e)] for l in b]
How to delete nan/null values in lists in a list in Python?
IF you want to keep rows with nans you can do it like this:
In [5457]: df.T.dropna(how='all').T
Out[5457]:
Index 1 2 3 4
0 20000765.000 624380.000 nan nan nan
1 20000766.000 624380.000 nan nan nan
2 20000768.000 1305984.000 1305985.000 1305983.000 1306021.000
if you don't want any columns with nans you can drop them like this:
In [5458]: df.T.dropna().T
Out[5458]:
Index 1
0 20000765.000 624380.000
1 20000766.000 624380.000
2 20000768.000 1305984.000
To create the array:
In [5464]: df.T.apply(lambda x: x.dropna().tolist()).tolist()
Out[5464]:
[[20000765.0, 624380.0],
[20000766.0, 624380.0],
[20000768.0, 1305984.0, 1305985.0, 1305983.0, 1306021.0]]
or
df.T[1:].apply(lambda x: x.dropna().tolist()).tolist()
Out[5471]: [[624380.0], [624380.0], [1305984.0, 1305985.0, 1305983.0, 1306021.0]]
depending on how you want the array
Python: How to remove empty lists from a list?
Try
list2 = [x for x in list1 if x != []]
If you want to get rid of everything that is "falsy", e.g. empty strings, empty tuples, zeros, you could also use
list2 = [x for x in list1 if x]
Related Topics
How to Change the Background Color of a Plot Made with Ggplot2
Fully Reproducible Parallel Models Using Caret
Promise Already Under Evaluation: Recursive Default Argument Reference or Earlier Problems
Set Certain Values to Na with Dplyr
Select Columns Based on String Match - Dplyr::Select
Techniques for Finding Near Duplicate Records
Plot Data in Descending Order as Appears in Data Frame
Merge Multiple Spaces to Single Space; Remove Trailing/Leading Spaces
How to Delete Columns That Contain Only Nas
Solution. How to Install_Github When There Is a Proxy
Issue When Importing Dataset: 'Error in Scan(...): Line 1 Did Not Have 145 Elements'
How to Generate Distributions Given, Mean, Sd, Skew and Kurtosis in R
Merge Data Frames Based on Rownames in R
Dplyr: Lead() and Lag() Wrong When Used with Group_By()