0
seq = [11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1]    
           
for i in range(len(seq)):
            print(seq[i],end ="\t")
        

How do I get my output table to look like this?

11  34  17  52  26  13  
40  20  10  5   16   8  
4   2   1
khelwood
  • 55,782
  • 14
  • 81
  • 108
kelpa17
  • 1
  • 1
  • use padding or string formatting https://stackoverflow.com/questions/9535954/printing-lists-as-tabular-data – Serge Nov 05 '20 at 20:48

3 Answers3

0

one of many ways is this, you make iterate over the seq list by a step of 6 and print the element between those margins

seq = [11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1]    

for i in range(0, len(seq), 6):
    print(*seq[i:i+6], sep=' ')

output

11 34 17 52 26 13
40 20 10 5 16 8
4 2 1
sahasrara62
  • 10,069
  • 3
  • 29
  • 44
0

The simplest relevant technique is padding

for i in range(0, len(seq), 6):
    print("  ".join[str(k).ljust(2, " ") for k in seq[i: i + 6]]

but string formatting as in Printing Lists as Tabular Data will make is a more sophisticated solution

Serge
  • 3,387
  • 3
  • 16
  • 34
0

You probably want to make use of string formatting. Below, f"{seq[i]:<4d}" means "A string of length 4, left-aligned, containing the string representation of seq[i]". If you want to right-align, just remove <.

seq = [11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1]

for i in range(len(seq)):
    print(f"{seq[i]:<4d}", end = "")

    if not (i+1) % 6:
        print("")

print("")

Output:

11  34  17  52  26  13
40  20  10  5   16  8
4   2   1
ThisIsAQuestion
  • 1,887
  • 14
  • 20