Print a list in columns in Python

avatar

Borislav Hadzhiev

Last updated: Sep 11, 2022

banner

Photo from Unsplash

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}')

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])))

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.

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}')

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}')

The same approach can be used if you need to print a list of lists in columns.

main.py
headers = [ 'ID', 'Name', 'Country' ] # 👇️ list of lists 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]}')
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.