L. Kappa L. Kappa - 1 month ago 6
Python Question

Prettytable make use of list

I have a 2 lists; one is called teamname and one scores. I want the values from those lists printed in PrettyTable. I know you can do it by t.add_row([scores[0]]), but it only takes the first item from the list (This way I have to know how many items are in a list and have to manually add the amount of rows). You can also do it by doing by entering t.add_row([scores]), but then it places the whole list in one line without entering a new row. Is it possible in PrettyTable to just enter the list and get a the data in it in new lines?

I've tried searching for this on the internet, but couldn't find anything.

Example 1:

t = PrettyTable(['Teamname', 'Scores'])
t.add_row([teams, scores])


Then you get:

+-------------------+--------+
| Teamname | Scores |
+-------------------+--------+
| ['test', 'test1'] | [1, 2] |
+-------------------+--------+


Example 2:

t = PrettyTable(['Teamname', 'Scores'])
t.add_row([teams[0], scores[0]])
t.add_row([teams[1], scores[1]])


Then you get:

+-------------------+--------+
| Teamname | Scores |
+-------------------+--------+
| test | [1] |
+-------------------+--------+
| test1 | [2] |
+-------------------+--------+

Answer

Why not add the data by columns:

t = PrettyTable()
t.add_column('Teamname', teams)
t.add_column('Scores', scores)
Comments