How can I remove empty elements from a list that contain zero length pairlist as character(0)
, integer(0)
etc...
list2 # $`hsa:7476` # [1] "1","2","3" # # $`hsa:656` # character(0) # # $`hsa:7475` # character(0) # # $`hsa:7472` # character(0)
I don't know how to deal with them. I mean if NULL
it is much simpler. How can I remove these elements such that just hsa:7476
remains in the list.
The easiest way is list comprehension to remove empty elements from a list in Python. And another way is to use the filter() method. The empty string "" contains no characters and empty elements could be None or [ ], etc.
Method #1: Using remove() This particular method is quite naive and not recommended use, but is indeed a method to perform this task. remove() generally removes the first occurrence of an empty string and we keep iterating this process until no empty string is found in list.
Another option(I think more efficient) by keeping index where element length > 0 :
l[lapply(l,length)>0] ## you can use sapply,rapply [[1]] [1] 1 2 3 [[2]] [1] "foo"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With