0

I have two lists

list1 = ['ipv6']
list2 = ['autodiscover', 'cdn0', 'dev', 'email', 'link', 'mail', 'shop', 'www']

Using the following print command and formatting I am trying to print the two lists in separate columns separated by the '|' character.


 print("{:<25} {:<1} {:<15}".format('\n'.join(list1), '|', '\n'.join(list2)))

The first item in each list properly aligned however all subsequent items from list2 are aligned to the left of the terminal.

Tags                      Subdomains
----------------------------------------------------------------------------------------------------
ipv6                      | autodiscover
cdn0
dev
email
link
mail
shop
www

Using the standard Python3 modules (no 3rd party modules) how would I align the other items in the list?

nate_
  • 5
  • 2
  • Does this answer your question? https://stackoverflow.com/a/26005077/1024832 – Marc Oct 22 '20 at 02:14
  • All the examples had lists of same length, and several of the solutions used 3rd party modules like pandas, tableit, and tabulate. thanks for looking though. – nate_ Oct 22 '20 at 02:20

1 Answers1

0

A bit hacky if you don't want to use other libraries but it'll work if you pad list1:

list1 = ['ipv6']
list2 = ['autodiscover', 'cdn0', 'dev', 'email', 'link', 'mail', 'shop', 'www']
pad = ['']*(len(list2)-len(list1))
for i,j in zip(list1+pad, list2):
    print(f"{i:<25} | {j:<15}")

Output:

ipv6                      | autodiscover   
                          | cdn0           
                          | dev            
                          | email          
                          | link           
                          | mail           
                          | shop           
                          | www            
Benedictanjw
  • 828
  • 1
  • 8
  • 19
  • That worked, thank you. I was able to have a properly aligned table with far less code than the route I was taking. – nate_ Oct 22 '20 at 02:45