I am trying to modify a list of two lists. For each of the two inside lists, I perform some operation and 'split' them into new lists.
Here is a simple example of what I'm trying to do:
[['a', 'b'], ['c', 'd']] --> [['a'], ['b'], ['c', 'd']]
Currently my algorithm passes ['a', 'b']
to a function that determines whether or not it should be split into [['a'], ['b']]
(e.g. based on their correlations). The function returns [['a'], ['b']]
which tells me that ['a', 'b']
should be split, or returns ['a', 'b']
(the original list) which indicates that it should not be split.
Currently I have something like this:
blist = [['a', 'b'], ['c', 'd']] #big list
slist = [['a'], ['b']] #small list returned by function
nlist = [items for i in xrange(len(blist)) for items in (slist if i==0 else blist[i])]
This produces [['a'], ['b'], 'c', 'd']
as opposed to the desired output [['a'], ['b'], ['c', 'd']]
which does not alter the second list in the original blist. I understand why this is happening--my second loop is also applied to blist[1]
in this case, but I am not sure how to fix it as I do not understand list comprehension completely.
A 'pythonic' solution is preferred. Any feedback would be appreciated, thank you!
EDIT: Like the title suggests, I am trying to 'replace' ['a', 'b']
with ['a'], ['b']
. So I would like the 'position' to be the same, having ['a'], ['b']
appear in the original list before ['c', 'd']
RESULTS Thank you Christian, Paul and schwobaseggl for your solutions! They all work :)