I have a list of 74 items, each of these items contain a list 1-10 items themselves.
My goal was to fill in any sublists that had less than 10 subitems to make them have 10 subitems each. The way I thought you did this was with zip_longest
from itertools
. However, when I do this, the resulting structure is 10 x 74 and I was expecting it to be 74 x 10. So this has done two things: it has filled in my shorter sublists which I wanted, but it has also performed a transpose operation at the same time. I only wanted the former.
So if I ran zip_longest
on the output of zip_longest
, this could perhaps give me the final output, but it seems I am missing something because why do I want to take 2 transposes? There has to be a cleaner way of padding these sublists.
This is the code I have
from itertools import zip_longest
zl = zip_longest(*master_results, fillvalue='')
for z1 in zl2:
print(len(z1))
zl2 = zip_longest(*zl, fillvalue='')
for z2 in zl2:
print(len(z2))
which gives me 10 printouts of 74 for the first for loop and 74 printouts of 10 for the 2nd for loop.
Here's a quick and straightforward approach.
for sublist in master_results:
if len(sublist) < 10:
sublist.extend([""]*(10-len(sublist)))