0

Honestly, I didn't find out for this question cuz... I don't know how to search or google it. So it might be the basic problem, but if anyone can helps me, I would really appreciate it.

So, the problem is... basically from the Index Error: list index out of range, which was raised while I tried to make a list of lists to excel file.

Apparently, the error raised cuz some of my list has 8 elements while some of my list has less elements than those.

for example:

results = [[1,2,3],[1,2,3,4],[1,2,3,4,5,6,7,8]]

and I tried to convert it as excel with xlsxwriter with this code:

for row in range(len(results)):
    for col in range(len(results[0])):
        sheet.write(row, col, results[row][col])

And now you know why the error was raised.

So, what I want to do about it is adding some blank elements to the less-element-having list to make all list has a same number of elements

like this:

results = [[1,2,3,'','','','',''],[1,2,3,4,'','','','',],[1,2,3,4,5,6,7,8]]

Thanks for your attention and helps in advance!

Jeong In Kim
  • 373
  • 2
  • 12

3 Answers3

3

Using zip with itertools.zip_longest:

import itertools

list(zip(*itertools.zip_longest(*results, fillvalue='')))

Output:

[(1, 2, 3, '', '', '', '', ''),
 (1, 2, 3, 4, '', '', '', ''),
 (1, 2, 3, 4, 5, 6, 7, 8)]
Chris
  • 29,127
  • 3
  • 28
  • 51
  • 1
    Thanks Chris. Can I ask what does * means with itertools and results? Sorry for the rookie question – Jeong In Kim Apr 02 '19 at 05:53
  • 1
    @JeongInKim Sure you can! `*` in this case _unpacks_ the iterables. In other words, `*results` becomes lists instead of single list of lists. [Reading the official document](https://docs.python.org/3/tutorial/controlflow.html#unpacking-argument-lists) may help you have better understanding of it :) – Chris Apr 02 '19 at 05:57
  • Ah-ha! Thanks Chris! You totally taught me to understand it ! I'll check the official document as well! Thanks for your kindness – Jeong In Kim Apr 02 '19 at 06:12
2

There are many ways to do this, hers's one:

results = [[1,2,3],[1,2,3,4],[1,2,3,4,5,6,7,8]]
m = max(len(l) for l in results)
results = [l + ['']*(m-len(l)) for l in results]
Julien
  • 13,986
  • 5
  • 29
  • 53
  • Thanks Julien ! I thought making this code would be complex and took long lines in my head xD Obviously, I was wrong. – Jeong In Kim Apr 02 '19 at 05:55
1

Rather than change the data to suit the loop it would be better to change the loop.

The enumerate() function is helpful for this:

import xlsxwriter

workbook = xlsxwriter.Workbook('test.xlsx')
worksheet = workbook.add_worksheet()

results = [[1,2,3],[1,2,3,4],[1,2,3,4,5,6,7,8]]

for row, row_data in enumerate(results):
    for col, data in enumerate(row_data):
        worksheet.write(row, col, data)

workbook.close()

Output:

enter image description here

jmcnamara
  • 38,196
  • 6
  • 90
  • 108
  • Wow that's another approach! I'll keep enumerate() function for the next time. It seems really helpful as you said! – Jeong In Kim Apr 03 '19 at 04:12