0

I'm trying to create a prettytable with 4 columns. The information I'm trying to insert per row are located in different python dictionary. I would assume the code below will work but i'm receiving the error below

Exception: Row has incorrect number of values, (actual) 2!=4 (expected)

My code is:

t = PrettyTable(['key', 'Start', 'End', 'Retention'])

for key, val in total.items():
    t.add_row([key, val])

for key, val in dic.items():
    t.add_row([key, val])

print(t)
bunji
  • 5,063
  • 1
  • 17
  • 36

2 Answers2

0

If you want to use values from both the total and dic dictionaries, You can use zip to iterate over them pairwise:

table = PrettyTable(['key', 'Start', 'End', 'Retention'])

for col_1_2, col_3_4 in zip(total.items(), dic.items()):
    table.add_row([*col_1_2, *col_3_4])
Christian Dean
  • 22,138
  • 7
  • 54
  • 87
0

your table is expecting 4 values for each row whereas you are entering only two values, that's the reason you are getting the error, so try this

 for (k,v), (k2,v2) in zip(total.items(), dic.items()):
        t.add_row([k, v, k2, v2])
arjunsv3691
  • 791
  • 6
  • 19