Print a list vertically in Python

avatar

Borislav Hadzhiev

Last updated: Sep 12, 2022

banner

Photo from Unsplash

Print a list vertically in Python #

To print a list vertically:

  1. Use a for loop to iterate over the list.
  2. Use a formatted string literal to 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]}')
The code sample prints a list of lists vertically. If you need to print a one-dimensional list vertically, scroll down to the next example.

The headers list is optional and gets printed right before the items in the employees list.

We used a formatted string literal to format each row.

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.

If you don't want to format each row, use the iterable unpacking operator in the call to print().

main.py
employees = [ [1, 'alice', 'Austria'], [2, 'bobbyhadz', 'Bulgaria'], [3, 'carl', 'Canada'], ] # 1 alice Austria # 2 bobbyhadz Bulgaria # 3 carl Canada for row in employees: print(*row)

The * iterable unpacking operator enables us to unpack an iterable in function calls, in comprehensions and in generator expressions.

If you need to print a one-dimensional list vertically, with each item on a separate line, use a simple for loop.

main.py
headers = ['ID', 'Name', 'Country'] for item in headers: # ID # Name # Country print(item)

You can use the zip() function if you need to print a one-dimensional list vertically, with formatting.

main.py
headers = ['ID', 'Name', 'Country'] my_list = [1, 'alice', 'Austria', 2, 'bobbyhadz', 'Bulgaria', 3, 'carl', 'Canada'] columns = 3 print(f'{headers[0]: <10}{headers[1]: <15}{headers[2]}') # ID Name Country # 1 alice Austria # 2 bobbyhadz Bulgaria # 3 carl Canada for first, second, third in zip(my_list[::columns], my_list[1::columns], my_list[2::columns]): print(f'{first: <10}{second: <15}{third}')

Printing headers is optional.

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
headers = ['ID', 'Name', 'Country'] my_list = [1, 'alice', 'Austria', 2, 'bobbyhadz', 'Bulgaria', 3, 'carl', 'Canada'] columns = 3 # [(1, 'alice', 'Austria'), (2, 'bobbyhadz', 'Bulgaria'), (3, 'carl', 'Canada')] 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 with every 3 elements in the list, starting at index 0, then at index 1, etc.

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.