Search code examples
pythondictionaryprettytable

Python: How to unpack dictionary keys as separate items rather than one item


When I run:

from prettytable import PrettyTable as PTable

accounts = {
    'user1': 'pass1',
    'user2': 'pass2'
}

table = PTable(['Day', [*accounts.keys()]])  #  creates empty table with headings
print(table)

My table looks like this:

+-----+--------------------+
| Day | ['user1', 'user2'] |
+-----+--------------------+
+-----+--------------------+

You can see that 'user1' and 'user2' are in the same column. How can I make it so that they are in separate columns?

Thank you.


Solution

  • You did unpack them as separate items... of the wrong list. If you wanted to unpack them into the same list as 'Day', don't write a nested list:

    ['Day', *accounts.keys()]