A = data.frame( a = c(1:10), b = c(11:20) )
B = data.frame( a = c(101:110), b = c(111:120) )
C = data.frame( a = c(5:8), b = c(55:58) )
L = list(list(B,C),list(A),list(C,A),list(A,B,C),list(C))
I have a list of lists of Data Frames, L but I have to creat a single List of all the Data Frames as below (The ordering of the dataframes should remain same in L and New L.)
NewL = list( B,C,A,C,A,A,B,C,C )
Using iteration This is the novel approach in which we take each element of the list and convert it to a format of lists. We use temporary list to achieve this. Finally all these elements which are converted to lists are group together to create the required list of lists.
Flatten List of Lists Using itertools (chain()) This approach is ideal for transforming a 2-D list into a single flat list as it treats consecutive sequences as a single sequence by iterating through the iterable passed as the argument in a sequential manner.
There are a number of ways to flatten a list of lists in python. You can use a list comprehension, the itertools library, or simply loop through the list of lists adding each item to a separate list, etc. Let's see them in action through examples followed by a runtime assessment of each.
Try reading the manual ;) and
unlist(L,recursive=F)
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