Python: I have a string and a list of lists of varying lengths and I want to return the letters in groups as they correspond to the list
<p>Imagine that I have a string that is 5 letters long, say 'ABCDE'.  I also have a list of lists of the different way to split the list i.e. [[5], [4, 1], [3, 2], ... [1, 1, 1, 1, 1]].  How could return all the different ways to split the list, like below.  The problem I am having is setting up a loop with uneven numbers of indices.</p>

<pre><code>INPUT
'ABCDE', list_of_configurations

OUTPUT
['ABCDE']
['A', 'BCDE']
['AB', 'CDE']
...
...
['A', 'B', 'C', 'D', 'E']
</code></pre>

<p>Final note, I want it be dynamic in that it can apply to a string of 5 characters or a string of 9 characters.  I was trying to figure this out but I think it is beyond my current skill level.</p>

<p>The previous answer is correct, but Python allows us to write this in much more compact fashion:</p>

<pre><code>def my_split(string, config):
    return [string[sum(config[:i]):sum(config[:i+1])] for i in range(len(config))]
</code></pre>

<p>To get the result for all configurations, you simply need to loop over the given configurations and call the my_split function.</p>

python string list