How to Print Lists in Columns and Tables in Python
Formatting data for display is a common task in programming.
This guide will explore various methods for printing lists in columns and creating tabular output in Python. We'll cover manual formatting with f-strings, as well as leveraging the tabulate
and prettytable
libraries for more complex use cases.
Printing Lists in Columns with zip()
and F-strings
You can use zip()
and list slicing to arrange a list into columns and format the output using f-strings:
my_list = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
columns = 3
for first, second, third in zip(
my_list[::columns], my_list[1::columns], my_list[2::columns]
):
print(f'{first: <10}{second: <10}{third}')
Output:
a b c
d e f
g h i
- The
zip()
function combines elements from multiple iterables into tuples. - The list slicing
my_list[::columns]
,my_list[1::columns]
andmy_list[2::columns]
create subsequences of the original list to be used in different columns. - The f-string formatting
{first: <10}{second: <10}{third}
creates the tabular format with left-aligned columns and a width of 10 characters.
1. Handling Variable Number of Columns
You can easily adjust the code to print a different number of columns by changing the value of the columns variable. Here's an example with two columns:
my_list = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
columns = 2
for first, second in zip(
my_list[::columns], my_list[1::columns]
):
print(f'{first: <10}{second}')
Output:
a b
c d
e f
g h
2. Printing Headers
You can also print headers before printing the columns:
headers = ['ID', 'Name', 'Country']
my_list = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
columns = 3
print(f'{headers[0]: <10}{headers[1]: <10}{headers[2]}')
for first, second, third in zip(
my_list[::columns], my_list[1::columns], my_list[2::columns]
):
print(f'{first: <10}{second: <10}{third}')
Output:
ID Name Country
a b c
d e f
g h i
Printing Lists of Lists in Columns with f-strings
When your data is in a list of lists, format each row with f-strings.
headers = ['ID', 'Name', 'Country']
employees = [
[1, 'alice', 'Austria'],
[2, 'anna', 'Italy'],
[3, 'carl', 'Canada'],
]
print(f'{headers[0]: <10}{headers[1]: <15}{headers[2]}')
for row in employees:
print(f'{row[0]: <10}{row[1]: <15}{row[2]}')
Output:
ID Name Country
1 alice Austria
2 anna Italy
3 carl Canada
- The f-string
{headers[0]: <10}{headers[1]: <15}{headers[2]}
formats the headers. - The for loop iterates over the list of rows and formats them.
Printing Tabular Data with tabulate
The tabulate
library offers a flexible and convenient way to print tabular data. To install it, run:
pip install tabulate
Here's how to use it:
from tabulate import tabulate
headers = ['ID', 'Name', 'Country']
employees = [
[1, 'alice', 'Austria'],
[2, 'cristiano', 'Portugal'],
[3, 'carl', 'Canada'],
]
print(tabulate(employees, headers=headers))
Output:
ID Name Country
---- --------- ---------
1 alice Austria
2 cristiano Portugal
3 carl Canada
The tabulate()
method is passed the data and the headers as arguments.
You can use various table formats (like github
, grid
and more):
from tabulate import tabulate
headers = [
'ID',
'Name',
'Country'
]
employees = [
[1, 'alice', 'Austria'],
[2, 'cristiano', 'Portugal'],
[3, 'carl', 'Canada'],
]
print(tabulate(employees, headers=headers, tablefmt='github'))
Output:
| ID | Name | Country |
|------|-----------|-----------|
| 1 | alice | Austria |
| 2 | cristiano | Portgual |
| 3 | carl | Canada |
Printing Tabular Data with prettytable
The prettytable
library provides another way to display data in tabular format. Install it using:
python -m pip install -U prettytable
Here is how to use the library:
from prettytable import PrettyTable
headers = ['ID', 'Name', 'Country']
employees = [
[1, 'alice', 'Austria'],
[2, 'cristiano', 'Portugal'],
[3, 'carl', 'Canada'],
]
pt = PrettyTable(headers)
pt.add_rows(employees)
print(pt)
Output:
+----+-----------+----------+
| ID | Name | Country |
+----+-----------+----------+
| 1 | alice | Austria |
| 2 | cristiano | Portugal |
| 3 | carl | Canada |
+----+-----------+----------+
The PrettyTable
class creates a table object to which you add rows.
You can also change the table style:
from prettytable import PrettyTable, MSWORD_FRIENDLY
headers = ['ID', 'Name', 'Country']
employees = [
[1, 'alice', 'Austria'],
[2, 'cristiano', 'Portugal'],
[3, 'carl', 'Canada'],
]
pt = PrettyTable(headers)
pt.set_style(MSWORD_FRIENDLY)
pt.add_rows(employees)
print(pt)
Output:
| ID | Name | Country |
| 1 | alice | Austria |
| 2 | cristiano | Portugal |
| 3 | carl | Canada |