I'm trying to divide a nested list into two nested lists using list comprehensions. I am unable to do so without converting the inner lists to strings, which in turn ruins my ability to access/print/control the values later on.
I tried this::
paragraphs3 = [['Page: 2', 'Bib: Something', 'Derived: This n that'], ['Page: 3', 'Bib: Something', 'Argument: Wouldn't you like to know?'], ...]
derived = [k for k in paragraphs3 if 'Derived:' in k]
therest = [k for k in paragraphs3 if 'Derived:' not in k]
What happens is that the whole of paragraphs3 = [] ends up in therest = [], unless i do something like this:
for i in paragraphs3:
i = str(i)
paragraphs4.append(i)
If I then feed paragraphs4 to the list comprehension, I get two lists, just like I want. But they are not nested lists anymore since this:
for i in therest:
g.write('\n'.join(i))
g.write('\n\n')
Writes each !character! in therest = [] in a separate line:
'
P
a
g
e
:
2
'
Thus I'm looking for a better way to split paragraphs3 ... Or maybe the solution lies elsewhere? The end result/output I'm looking for is:
Page: 2
Bib: Something
Derived: This n that
Page: 3
Bib: Something
.
.
.