2

I have a list of 107 names, I would like to print them out in groups of 3 or so, with each name separated by a tab, a newline after each line, until the end. How can I do this?

with for item in list print item i only get 1 name per line of course, which is fine I guess but i'd like to fit more in the console at once so I'd like to print 3 or so names on each line as I go through the list, so instead of:

name1
name2
name3
name4
name5
name6

i would get:

name1     name2     name3
name4     name5     name6

It's kindof hard to search for an answer to this, i haven't been able to come up with quite what I need or that I could understand, most things I did find just deal with len() or range() and confused me. Is there some simple way to do this? Thank you!

[edit:update] using @inspectorG4dget's example of:

for i in range(0, len(listnames), 5):
    print '\t\t'.join(listnames[i:i+5])

i get the following: http://www.pasteall.org/pic/show.php?id=41159

how can I get that cleaned up so everything is nicely aligned in each column? Is what I want possible to do easily?

Kassandra
  • 271
  • 1
  • 6
  • 14
  • related: [What is the most “pythonic” way to iterate over a list in chunks?](http://stackoverflow.com/questions/434287/what-is-the-most-pythonic-way-to-iterate-over-a-list-in-chunks) – jfs Dec 01 '12 at 01:10
  • if you just need to format the list for console; you could use [`textwrap.fill("\t".join(lst))`](http://ideone.com/lxrJ4Z) – jfs Dec 01 '12 at 01:27
  • @J.F. Sebastian perhaps that would work but it doesn't get me a very clean output from what I can see here since my names are all different lengths – Kassandra Dec 01 '12 at 01:52
  • it seems you want [`column(1)` command](http://linux.die.net/man/1/column). – jfs Dec 02 '12 at 19:39

4 Answers4

5

1)

li = ['sea','mountain','desert',
      'Emma','Cathy','Kate',
      'ii','uuuuuuuuuuuuuuuuuuu','aaa',
      'round','flat','sharp',
      'blueberry','banana','apple',
      'red','purple','white',
      'hen','tiger']

a,b = divmod(len(li),3)
itn = iter(li).next
print ''.join('%s\t%s\t%s\n' % (itn(),itn(),itn())
              for i in xrange(a))\
      + ('%s\t%s\t\n' % (itn(),itn()) if b==2
         else '%s\t\n' % itn() if b==1
         else '')

result

sea mountain    desert
Emma    Cathy   Kate
ii  uuuuuuuuuuuuuuuuuuu aaa
round   flat    sharp
blueberry   banana  apple
red purple  white
hen tiger   

.

2)

And to align in columns whose width depends on the longest element of the list:

li = ['sea','mountain','desert',
      'Emma','Cathy','Kate',
      'HH','VVVVVVV','AAA',
      'round','flat','sharp',
      'blueberry','banana','apple',
      'red','purple','white',
      'hen','tiger']

maxel = max(len(el) for el in li)
a,b = divmod(len(li),3)
itn = iter(li).next
form = '%%-%ds\t%%-%ds\t%%-%ds\n' % (maxel,maxel,maxel)
print ''.join(form % (itn(),itn(),itn())
              for i in xrange(a))\
      + ('%%-%ds\t%%-%ds\t\n' %(maxel,maxel) % (itn(),itn()) if b==2
         else '%%-%ds\t\n' % ma% itn() if b==1
         else '')

result

sea         mountain    desert   
Emma        Cathy       Kate     
HH          VVVVVVV     AAA      
round       flat        sharp    
blueberry   banana      apple    
red         purple      white    
hen         tiger       

.

3)

To align in column, the width of each column depending upon the longest element in it:

li = ['sea','mountain','desert',
      'Emma','Cathy','Kate',
      'HH','VVVVVVV','AAA',
      'round','flat','sharp',
      'nut','banana','apple',
      'red','purple','white',
      'hen','tiger']

maxel0 = max(len(li[i]) for i in xrange(0,len(li),3)) 
maxel1 = max(len(li[i]) for i in xrange(1,len(li),3))
maxel2 = max(len(li[i]) for i in xrange(2,len(li),3))
a,b = divmod(len(li),3)
itn = iter(li).next
form = '%%-%ds\t%%-%ds\t%%-%ds\n' % (maxel0,maxel1,maxel2)
print ''.join(form % (itn(),itn(),itn())
              for i in xrange(a))\
      + ('%%-%ds\t%%-%ds\t\n' %(maxel0,maxel1) % (itn(),itn()) if b==2
         else '%%-%ds\t\n' % maxel0 % itn() if b==1
         else '')

result

sea     mountain    desert
Emma    Cathy       Kate  
HH      VVVVVVV     AAA   
round   flat        sharp 
nut     banana      apple 
red     purple      white 
hen     tiger       

4)

I've modified the algorithm in order to generalize to any number of columns wanted.
The wanted number of columns must be passed as argument to parameter nc :

from itertools import imap,islice

li = ['sea','mountain','desert',
      'Emma','Cathy','Kate',
      'HH','VVVVVVV','AAA',
      'round','flat','sharp',
      'nut','banana','apple',
      'heeeeeeeeeeen','tiger','snake'
      'red','purple','white',
      'atlantic','pacific','antarctic',
      'Bellini']

print 'len of li == %d\n' % len(li)

def cols_print(li,nc):
    maxel = tuple(max(imap(len,islice(li,st,None,nc)))
                  for st in xrange(nc))

    nblines,tail = divmod(len(li),nc)
    stakes = (nc-1)*['%%-%ds\t'] + ['%%-%ds']
    form = ''.join(stakes) % maxel

    itn = iter(li).next

    print '\n'.join(form % tuple(itn() for g in xrange(nc))
                  for i in xrange(nblines)) 
    if tail:
        print ''.join(stakes[nc-tail:]) % maxel[0:tail] % tuple(li[-tail:]) + '\n'
    else:
        print


for nc in xrange(3,8):
    cols_print(li,nc)
    print '-----------------------------------------------------------'

result

len of li == 24

sea             mountain    desert  
Emma            Cathy       Kate    
HH              VVVVVVV     AAA     
round           flat        sharp   
nut             banana      apple   
heeeeeeeeeeen   tiger       snakered
purple          white       atlantic
pacific         antarctic   Bellini 

-----------------------------------------------------------
sea         mountain    desert      Emma         
Cathy       Kate        HH          VVVVVVV      
AAA         round       flat        sharp        
nut         banana      apple       heeeeeeeeeeen
tiger       snakered    purple      white        
atlantic    pacific     antarctic   Bellini      

-----------------------------------------------------------
sea             mountain    desert      Emma    Cathy
Kate            HH          VVVVVVV     AAA     round
flat            sharp       nut         banana  apple
heeeeeeeeeeen   tiger       snakered    purple  white
atlantic        pacific     antarctic   Bellini

-----------------------------------------------------------
sea     mountain    desert      Emma            Cathy       Kate    
HH      VVVVVVV     AAA         round           flat        sharp   
nut     banana      apple       heeeeeeeeeeen   tiger       snakered
purple  white       atlantic    pacific         antarctic   Bellini 

-----------------------------------------------------------
sea     mountain        desert  Emma        Cathy   Kate    HH      
VVVVVVV AAA             round   flat        sharp   nut     banana  
apple   heeeeeeeeeeen   tiger   snakered    purple  white   atlantic
pacific antarctic       Bellini

-----------------------------------------------------------

.

But I prefer a displaying in which there are no tabs between columns, but only a given number of characters.
In the following code, I choosed to separate the columns by 2 characters: it is the 2 in the line

maxel = tuple(max(imap(len,islice(li,st,None,nc)))+2

The code

from itertools import imap,islice

li = ['sea','mountain','desert',
      'Emma','Cathy','Kate',
      'HH','VVVVVVV','AAA',
      'round','flat','sharp',
      'nut','banana','apple',
      'heeeeeeeeeeen','tiger','snake'
      'red','purple','white',
      'atlantic','pacific','antarctic',
      'Bellini']

print 'len of li == %d\n' % len(li)
def cols_print(li,nc):
    maxel = tuple(max(imap(len,islice(li,st,None,nc)))+2
                  for st in xrange(nc))

    nblines,tail = divmod(len(li),nc)
    stakes = nc*['%%-%ds']
    form = ''.join(stakes) % maxel

    itn = iter(li).next

    print '\n'.join(form % tuple(itn() for g in xrange(nc))
                  for i in xrange(nblines)) 
    if tail:
        print ''.join(stakes[nc-tail:]) % maxel[0:tail] % tuple(li[-tail:]) + '\n'
    else:
        print


for nc in xrange(3,8):
    cols_print(li,nc)
    print 'mwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwm'

the result

len of li == 24

sea            mountain   desert    
Emma           Cathy      Kate      
HH             VVVVVVV    AAA       
round          flat       sharp     
nut            banana     apple     
heeeeeeeeeeen  tiger      snakered  
purple         white      atlantic  
pacific        antarctic  Bellini   

mwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwm
sea       mountain  desert     Emma           
Cathy     Kate      HH         VVVVVVV        
AAA       round     flat       sharp          
nut       banana    apple      heeeeeeeeeeen  
tiger     snakered  purple     white          
atlantic  pacific   antarctic  Bellini        

mwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwm
sea            mountain  desert     Emma     Cathy  
Kate           HH        VVVVVVV    AAA      round  
flat           sharp     nut        banana   apple  
heeeeeeeeeeen  tiger     snakered   purple   white  
atlantic       pacific   antarctic  Bellini  

mwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwm
sea     mountain  desert    Emma           Cathy      Kate      
HH      VVVVVVV   AAA       round          flat       sharp     
nut     banana    apple     heeeeeeeeeeen  tiger      snakered  
purple  white     atlantic  pacific        antarctic  Bellini   

mwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwm
sea      mountain       desert   Emma      Cathy   Kate   HH        
VVVVVVV  AAA            round    flat      sharp   nut    banana    
apple    heeeeeeeeeeen  tiger    snakered  purple  white  atlantic  
pacific  antarctic      Bellini  

mwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwm
eyquem
  • 26,771
  • 7
  • 38
  • 46
  • 1
    oh goodness this looks like exactly what i want thank you very much :) the cake goes to you – Kassandra Dec 01 '12 at 03:30
  • any way i could get this to be 5 columns instead of 3? i tried changing some of the numbers, adding another element definition, and fixing the string format but it's erroring – Kassandra Dec 01 '12 at 04:21
  • i think i figured it out, i have it printing 5's now, which is just a tiny bit below 80 chars wide so thats good, thank you for your help :) – Kassandra Dec 01 '12 at 04:40
  • i seem to have found a bug of sorts with this method, in some cases, if the length of my list of names changes, some at the end will not be printed, I think it has something to do with the math divmod is doing. Any fix? – Kassandra Dec 01 '12 at 07:53
  • @Kassandra I will examine the problems, but could you describe them more, please ? – eyquem Dec 01 '12 at 08:11
  • in the case of my 108 name long list, the last 4 items are not being printed, when i've set up the code to print rows of 5 using your 2nd method. Also when using it unmodified, if I give it 112 names it errors `Traceback (most recent call last): File "trylayout.py", line 53, in else '%%-%ds\t\n' % ma% itn() if b==1 NameError: name 'ma' is not defined` but not with 1 less or 1 more – Kassandra Dec 01 '12 at 09:06
  • @Kassandra With a list of 108 elements, and a wanted number of columns nc = 5 , there is a tail of 3 elements, not 4, because 108 == 21 * 5(nc) + 3(tail) . Then the object **b** in my second code has value 3, and when the conditions ``if b==2`` and ``if b==1`` are tested, their results are False then it prints ``''`` – eyquem Dec 01 '12 at 11:21
  • @Kassandra With code 2 unmodified, that is to say with nc = 3, used on a list of 112 elements, we have 112 == 37 * 3(nc) + 1(tail). So in this case, **b** has value 1 and you can see that there is an error in ``else '%%-%ds\t\n' % ma% itn() if b==1`` since there is no object defined with name ``ma`` in the second code. Hence the traceback that says the truth. Error messages are made to be useful and analyzed. – eyquem Dec 01 '12 at 11:28
2

This should do it:

In [12]: L
Out[12]: ['name1', 'name2', 'name3', 'name4', 'name5', 'name6']

In [13]: for i in range(0,len(L),3): print ' '.join(L[i:i+3])
name1 name2 name3
name4 name5 name6

EDIT: to get everything into a fixed width (some code that I wrote a while to turn columnar data into a table. All you have to do is columnize your data and call this old code):

def tabularize(infilepath, outfilepath, delim='\t', largeFile=False):
        """ Return nothing
                Write into the file in outfilepath, the contents of infilepath, expressed in tabular form.
                The tabular form is similar to the way in which SQL tables are displayed.
                If largeFile is set to True, then no caching of lines occurs. However, two passes of the infile are required"""

        if largeFile:
                widths = getWidths(infilepath, delim)
        else:
                with open(infilepath) as infile:
                        lines = [line.strip().split(delim) for line in infile.readlines() if line.strip()]
                widths = [max([len(row) for row in rows])+2 for rows in izip_longest(*lines, fillvalue="")]

                with open(outfilepath, 'w') as outfile:
                        outfile.write("+")
                        for width in widths:
                                outfile.write('-'*width + "+")
                        outfile.write('\n')
                        for line in lines:
                                outfile.write("|")
                                for col,width in izip_longest(line,widths, fillvalue=""):
                                        outfile.write("%s%s%s|" %(' '*((width-len(col))/2), col, ' '*((width+1-len(col))/2)))
                                outfile.write('\n+')
                                for width in widths:
                                        outfile.write('-'*width + "+")
                                outfile.write('\n')

def getWidths(infilepath, delim):
        answer = defaultdict(int)
        with open(infilepath) as infile:
                for line in infile:
                        cols = line.strip().split(delim)
                        lens = map(len, cols)
                        for i,l in enumerate(lens):
                                if answer[i] < l:
                                        answer[i] = l

        return [answer[k] for k in sorted(answer)]

def main(L, n, infilepath, outfilepath):
    iterator = iter(L)
    with open(infilepath, 'w') as infile:
        for row in itertools.izip_longest([iterator]*n, fillavalue=''):
            infile.write('\t'.join(row)+'\n')
    if len(L) > 10**6:
        largeFile = True
    tabularize(infilepath, outfilepath, delim='\t', largeFile)
inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241
  • this does indeed do it though it's not quite what i had in mind in terms of clean alignment, my names have varying lengths, some are aligned in a column othere are not, is there any way to get this to ensure things will be aligned like a table? – Kassandra Dec 01 '12 at 01:56
  • oh gosh this is more complicated then I thought lol, thank you for the big code there I'll give it a try. – Kassandra Dec 01 '12 at 02:51
2

You could also try this:

from itertools import izip

l = ['name1', 'name2', 'name3', 'name4', 'name5', 'name6']

for t in izip(*[iter(l)]*3):
    print '\t'.join(t)
name1   name2   name3
name4   name5   name6

If you're not certain that the list length will be a multiple of 3, you could use izip_longest, applying the same idea:

from itertools import izip_longest as izipl

l = ['name1', 'name2', 'name3', 'name4', 'name5', 'name6', 'name7']

for t in izipl(fillvalue='', *[iter(l)]*3):
    print '\t'.join(t)
name1   name2   name3
name4   name5   name6
name7   
arshajii
  • 127,459
  • 24
  • 238
  • 287
1

Try using itertools i think its a much more simpler solution.

from itertools import izip_longest

def grouper(n, iterable, fillvalue=None):
    args = [iter(iterable)] * n
    return izip_longest(fillvalue=fillvalue, *args)

names = ['name1', 'name2', 'name3', 'name4', 'name5', 'name6']
for item1 in grouper(3, names, ''):
    print '\t'.join(item1)

Result:

name1   name2   name3
name4   name5   name6