How to group list with index wise?
<p>I have a list of list where i need to group elements by using user input ( see <code>split</code> variable in code ) and create new list.
I have tried but instead of grouping, elements are concatenated separately </p>

<pre><code>split = 3 # user input 
data = [[1,2], [3,4], [5,6], [7,8], [9,10], [11,12], [13,14], [15,16], [17,18]]
z = [] ; y = []
for i,d in enumerate(data):
    z.append(d)
    if (i+1)%split==0:
        y.append(z)
        z = []
xx = (y+[z])
print(xx)
[[[1, 2], [3, 4], [5, 6]], [[7, 8], [9, 10], [11, 12]], [[13, 14], [15, 16], [17, 18]], []]
# ^____________________^    ^_______________________^  this needs to be merged
</code></pre>

<p>input :</p>

<pre><code>data = [[1,2], [3,4], [5,6], [7,8], [9,10], [11,12], [13,14], [15,16], [17,18]]
</code></pre>

<p>expected output :</p>

<pre><code>[[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12], [13, 14, 15, 16, 17, 18]]
</code></pre>

<p>You can do this in basic Python like this:</p>

<pre><code>import itertools
flatlist = [*itertools.chain(*data)]
groupsize = int(len(flatlist) / split)

data2 = [flatlist[i:i+groupsize] for i in range(0, len(flatlist), groupsize)]
print(data2)
</code></pre>

<p>And the output is</p>

<pre><code>[[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12], [13, 14, 15, 16, 17, 18]]
</code></pre>

python list group