BlandCorporation
BlandCorporation

Reputation: 1344

How can a Python list be printed in columns?

I want to display a simple list in a specified number of aligned columns. For example, the following is the list displayed in 14 columns, with the numbers of spaces between columns varying between 3 and 1:

a = [
         1,    2,   3,   4,   5,   6,   7,   8,   9,  10,  11,  12,  13,  14,
        15,   16,  17,  18,  19,  20,  21,  22,  23,  24,  25,  26,  27,  28,
        29,   30,  31,  32,  33,  34,  35,  36,  37,  38,  39,  40,  41,  42,
        43,   44,  45,  46,  47,  48,  49,  50,  51,  52,  53,  54,  55,  56,
        57,   58,  59,  60,  61,  62,  63,  64,  65,  66,  67,  68,  69,  70,
        71,   72,  73,  74,  75,  76,  77,  78,  79,  80,  81,  82,  83,  84,
        85,   86,  87,  88,  89,  90,  91,  92,  93,  94,  95,  96,  97,  98,
        99,  100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112,
        113, 114, 115, 116, 117, 118, 119, 120
    ]

My initial attempt is as follows:

for a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14 in zip(*[iter(a)] * 14):
    print(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14)

However, this results in misaligned columns and missing list elements:

1 2 3 4 5 6 7 8 9 10 11 12 13 14
15 16 17 18 19 20 21 22 23 24 25 26 27 28
29 30 31 32 33 34 35 36 37 38 39 40 41 42
43 44 45 46 47 48 49 50 51 52 53 54 55 56
57 58 59 60 61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80 81 82 83 84
85 86 87 88 89 90 91 92 93 94 95 96 97 98
99 100 101 102 103 104 105 106 107 108 109 110 111 112

How could this be done?

Upvotes: 1

Views: 92

Answers (5)

Martin Evans
Martin Evans

Reputation: 46759

The following works out the largest number of digits required to pad each entry. So in this case 3 are required. If 1000 was added to the list, this would become 4.

from itertools import izip_longest    
import math        

a = [
     1,    2,   3,   4,   5,   6,   7,   8,   9,  10,  11,  12,  13,  14,
    15,   16,  17,  18,  19,  20,  21,  22,  23,  24,  25,  26,  27,  28,
    29,   30,  31,  32,  33,  34,  35,  36,  37,  38,  39,  40,  41,  42,
    43,   44,  45,  46,  47,  48,  49,  50,  51,  52,  53,  54,  55,  56,
    57,   58,  59,  60,  61,  62,  63,  64,  65,  66,  67,  68,  69,  70,
    71,   72,  73,  74,  75,  76,  77,  78,  79,  80,  81,  82,  83,  84,
    85,   86,  87,  88,  89,  90,  91,  92,  93,  94,  95,  96,  97,  98,
    99,  100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112,
    113, 114, 115, 116, 117, 118, 119, 120
    ]

pad = '{{:{}}}'.format(int(math.ceil(math.log(max(a)+1) / math.log(10))))

for row in izip_longest(*[iter(a)] * 14, fillvalue=''):
    print ' '.join(pad.format(v) for v in row)

izip_longest is used with a fill value to pad any remaining entries. This would give you the following output:

  1   2   3   4   5   6   7   8   9  10  11  12  13  14
 15  16  17  18  19  20  21  22  23  24  25  26  27  28
 29  30  31  32  33  34  35  36  37  38  39  40  41  42
 43  44  45  46  47  48  49  50  51  52  53  54  55  56
 57  58  59  60  61  62  63  64  65  66  67  68  69  70
 71  72  73  74  75  76  77  78  79  80  81  82  83  84
 85  86  87  88  89  90  91  92  93  94  95  96  97  98
 99 100 101 102 103 104 105 106 107 108 109 110 111 112
113 114 115 116 117 118 119 120

If your list of numbers for example only went up to say 50, it would automatically adjust as follows:

 1  2  3  4  5  6  7  8  9 10 11 12 13 14
15 16 17 18 19 20 21 22 23 24 25 26 27 28
29 30 31 32 33 34 35 36 37 38 39 40 41 42
43 44 45 46 47 48 49 50

Upvotes: 1

quantummind
quantummind

Reputation: 2136

Another simple solution without itertools:

i=0
while i<len(a):
  for j in range(i,min(i+14,len(a))):
    print '%4d' % (a[j]),
  print
  i+=14

Upvotes: 1

Max Jonatan Karlsson
Max Jonatan Karlsson

Reputation: 347

This is not a beautiful solution, but you could add spaces according to the string length of the integer:

a = [
         1,    2,   3,   4,   5,   6,   7,   8,   9,  10,  11,  12,  13,  14,
        15,   16,  17,  18,  19,  20,  21,  22,  23,  24,  25,  26,  27,  28,
        29,   30,  31,  32,  33,  34,  35,  36,  37,  38,  39,  40,  41,  42,
        43,   44,  45,  46,  47,  48,  49,  50,  51,  52,  53,  54,  55,  56,
        57,   58,  59,  60,  61,  62,  63,  64,  65,  66,  67,  68,  69,  70,
        71,   72,  73,  74,  75,  76,  77,  78,  79,  80,  81,  82,  83,  84,
        85,   86,  87,  88,  89,  90,  91,  92,  93,  94,  95,  96,  97,  98,
        99,  100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112,
        113, 114, 115, 116, 117, 118, 119, 120
    ]


maxLen = len(str(max(a)))
a = [str(A)+" "*(maxLen-len(str(A))) for A in a]

for a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14 in zip(*[iter(a)] * 14):
    print(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14)

Output:

1   2   3   4   5   6   7   8   9   10  11  12  13  14 
15  16  17  18  19  20  21  22  23  24  25  26  27  28 
29  30  31  32  33  34  35  36  37  38  39  40  41  42 
43  44  45  46  47  48  49  50  51  52  53  54  55  56 
57  58  59  60  61  62  63  64  65  66  67  68  69  70 
71  72  73  74  75  76  77  78  79  80  81  82  83  84 
85  86  87  88  89  90  91  92  93  94  95  96  97  98 
99  100 101 102 103 104 105 106 107 108 109 110 111 112

Upvotes: 3

Moses Koledoye
Moses Koledoye

Reputation: 78556

You could use some string formatting:

from itertools import izip_longest

width = 14  
for lst in izip_longest(*[iter(a)]*width, fillvalue=''):
  print(('{:3} '*width).format(*lst))

Remember to use itertools.izip_longest instead of zip so all the items in the list are printed, solving the missing items problem.


  1   2   3   4   5   6   7   8   9  10  11  12  13  14 
 15  16  17  18  19  20  21  22  23  24  25  26  27  28 
 29  30  31  32  33  34  35  36  37  38  39  40  41  42 
 43  44  45  46  47  48  49  50  51  52  53  54  55  56 
 57  58  59  60  61  62  63  64  65  66  67  68  69  70 
 71  72  73  74  75  76  77  78  79  80  81  82  83  84 
 85  86  87  88  89  90  91  92  93  94  95  96  97  98 
 99 100 101 102 103 104 105 106 107 108 109 110 111 112 
113 114 115 116 117 118 119 120 

Upvotes: 3

akuiper
akuiper

Reputation: 214957

Adding a tab between elements and then printing seem to work for me:

for i in range(0, len(a)-1, 14):
    print('\t'.join(map(str, a[i:i+14])))

1   2   3   4   5   6   7   8   9   10  11  12  13  14
15  16  17  18  19  20  21  22  23  24  25  26  27  28
29  30  31  32  33  34  35  36  37  38  39  40  41  42
43  44  45  46  47  48  49  50  51  52  53  54  55  56
57  58  59  60  61  62  63  64  65  66  67  68  69  70
71  72  73  74  75  76  77  78  79  80  81  82  83  84
85  86  87  88  89  90  91  92  93  94  95  96  97  98
99  100 101 102 103 104 105 106 107 108 109 110 111 112
113 114 115 116 117 118 119 120

Upvotes: 3

Related Questions