How to Print a List in Columns in Python

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
6 min

banner

# Table of Contents

  1. Print a List in Columns in Python
  2. Print a List of Lists in columns in Python
  3. Print a List in Tabular format using tabulate
  4. Print a List in Tabular format using prettytable

# Print a list in columns in Python

To print a list in columns:

  1. Use the zip() function to get a zip object of tuples.
  2. Use a formatted string literal to format the items in each tuple in a row.
  3. Use the print() function to print the result.
main.py
my_list = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'] columns = 3 # a b c # d e f # g h i for first, second, third in zip( my_list[::columns], my_list[1::columns], my_list[2::columns] ): print(f'{first: <10}{second: <10}{third}')

print list in columns

The code for this article is available on GitHub

The zip function iterates over several iterables in parallel and produces tuples with an item from each iterable.

The zip function returns an iterator of tuples.

main.py
my_list = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'] columns = 3 # ๐Ÿ‘‡๏ธ [('a', 'b', 'c'), ('d', 'e', 'f'), ('g', 'h', 'i')] print( list( zip( my_list[::columns], my_list[1::columns], my_list[2::columns]) ) )

zip function returns an iterator of tuples

We used list slicing in the call to the zip() function.

main.py
my_list = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'] print(my_list[::3]) # ๐Ÿ‘‰๏ธ ['a', 'd', 'g'] print(my_list[1::3]) # ๐Ÿ‘‰๏ธ ['b', 'e', 'h'] print(my_list[2::3]) # ๐Ÿ‘‰๏ธ ['b', 'e', 'h']

The syntax for list slicing is my_list[start:stop:step].

We specified the step value to get a list containing every 3 elements because we want to print 3 columns.

The first list slice contains every 3 elements of the original list starting at index 0.

The second list slice contains every 3 elements of the original list starting at index 1.

The last step is to use a formatted string literal to format the list items in columns.

main.py
my_list = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'] columns = 3 # a b c # d e f # g h i for first, second, third in zip( my_list[::columns], my_list[1::columns], my_list[2::columns] ): print(f'{first: <10}{second: <10}{third}')

Formatted string literals also enable us to use the format-specific mini-language in expression blocks.

main.py
my_str = 'hi' # ๐Ÿ‘‡๏ธ left-aligned result = f'{my_str: <6}' print(repr(result)) # ๐Ÿ‘‰๏ธ 'hi ' # ๐Ÿ‘‡๏ธ right-aligned result = f'{my_str: >6}' print(repr(result)) # ๐Ÿ‘‰๏ธ ' hi'

The space between the colon and the less-than sign is the fill character.

The less-than or greater-than sign is the alignment.

The less-than sign aligns the string to the left and the greater-than sign aligns the string to the right.

# Printing the list in 2 columns

You can print the list in two columns with some very minor changes.

main.py
my_list = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'] columns = 2 # a b # c d # e f # g h for first, second in zip( my_list[::columns], my_list[1::columns] ): print(f'{first: <10}{second}')

print list in 2 columns

The code for this article is available on GitHub

# Printing the headers of the list as well

You can also print the headers before you print any of the columns.

main.py
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]}') # ID Name Country # a b c # d e f # g h i for first, second, third in zip( my_list[::columns], my_list[1::columns], my_list[2::columns] ): print(f'{first: <10}{second: <10}{third}')

printing headers of list as well

# Print a List of Lists in columns in Python

To print a list in tabular format:

  1. Use a formatted string literal to print the headers.
  2. Iterate over the list and format each row.
  3. Use the print() function to print the result.
main.py
headers = [ 'ID', 'Name', 'Country' ] employees = [ [1, 'alice', 'Austria'], [2, 'bobbyhadz', 'Bulgaria'], [3, 'carl', 'Canada'], ] print(f'{headers[0]: <10}{headers[1]: <15}{headers[2]}') # ID Name Country # 1 alice Austria # 2 bobbyhadz Bulgaria # 3 carl Canada for row in employees: print(f'{row[0]: <10}{row[1]: <15}{row[2]}')

print list of lists in columns

The code for this article is available on GitHub
The example shows how to print a list of lists in tabular format, if you have a one-dimensional list, scroll down to the next example.

We used a formatted string literal to format the headers and rows.

Formatted string literals also enable us to use the format-specific mini-language in expression blocks.

main.py
my_str = 'hi' # ๐Ÿ‘‡๏ธ left-aligned result = f'{my_str: <6}' print(repr(result)) # ๐Ÿ‘‰๏ธ 'hi ' # ๐Ÿ‘‡๏ธ right-aligned result = f'{my_str: >6}' print(repr(result)) # ๐Ÿ‘‰๏ธ ' hi'

The space between the colon and the less-than sign is the fill character.

The less-than or greater-than sign is the alignment.

The less-than sign aligns the string to the left and the greater-than sign aligns the string to the right.

We first format and print the headers and then iterate over the list and print each row.

# Print a list in Tabular format using tabulate

  1. Run the pip install tabulate command to install the tabulate module.
  2. Pass the list as an argument to the tabulate() method.

First, open your terminal in your project's root directory and install the tabulate module.

shell
pip install tabulate # ๐Ÿ‘‡๏ธ or with pip3 pip3 install tabulate

Now we can import and use the tabulate method.

main.py
from tabulate import tabulate headers = [ 'ID', 'Name', 'Country' ] employees = [ [1, 'alice', 'Austria'], [2, 'bobbyhadz', 'Bulgaria'], [3, 'carl', 'Canada'], ] # ID Name Country # ---- --------- --------- # 1 alice Austria # 2 bobbyhadz Bulgaria # 3 carl Canada print(tabulate(employees, headers=headers))
The code for this article is available on GitHub

As the documentation states, the following tabular data types are supported:

  • list of lists or another iterable of iterables
  • list or another iterable of dictionaries (keys as columns)
  • dictionary of iterables (keys as columns)
  • two-dimensional Numpy arrays
  • NumPy record arrays (names as columns)
  • pandas.DataFrame

The tabulate() method also takes a tablefmt argument that can be used to format the table.

main.py
from tabulate import tabulate headers = [ 'ID', 'Name', 'Country' ] employees = [ [1, 'alice', 'Austria'], [2, 'bobbyhadz', 'Bulgaria'], [3, 'carl', 'Canada'], ] # | ID | Name | Country | # |------|-----------|-----------| # | 1 | alice | Austria | # | 2 | bobbyhadz | Bulgaria | # | 3 | carl | Canada | print(tabulate(employees, headers=headers, tablefmt='github'))

If you need to test some of the other options, scroll down to the "Table format" section in the documentation.

You can also set the headers argument to "firstrow" if your headers are stored in the first row of the list.

# Print a list in Tabular format using prettytable

Alternatively, you can use the prettytable module.

  1. Run the pip install prettytable command to install the prettytable module.
  2. Use the PrettyTable class from the module to print the list in tabular format.

First, open your terminal and install the prettytable module.

shell
python -m pip install -U prettytable # ๐Ÿ‘‡๏ธ or python3 python3 -m pip install -U prettytable

Now we can import and use the PrettyTable class.

main.py
from prettytable import PrettyTable headers = [ 'ID', 'Name', 'Country' ] employees = [ [1, 'alice', 'Austria'], [2, 'bobbyhadz', 'Bulgaria'], [3, 'carl', 'Canada'], ] pt = PrettyTable(headers) pt.add_rows(employees) # +----+-----------+----------+ # | ID | Name | Country | # +----+-----------+----------+ # | 1 | alice | Austria | # | 2 | bobbyhadz | Bulgaria | # | 3 | carl | Canada | # +----+-----------+----------+ print(pt)

The prettytable module can also be used for sorting.

You can also change the formatting of the table.

main.py
from prettytable import PrettyTable, MSWORD_FRIENDLY headers = [ 'ID', 'Name', 'Country' ] employees = [ [1, 'alice', 'Austria'], [2, 'bobbyhadz', 'Bulgaria'], [3, 'carl', 'Canada'], ] pt = PrettyTable(headers) pt.set_style(MSWORD_FRIENDLY) pt.add_rows(employees) # | ID | Name | Country | # | 1 | alice | Austria | # | 2 | bobbyhadz | Bulgaria | # | 3 | carl | Canada | print(pt)

You can view some of the other formatting options in the "Setting a table style" section of the documentation.

I've also written an article on how to print a dictionary in table format.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev