def columnate(l, prefix):
 if (not l):
  return ''
 l = l[:]
 clen = max((len(s) for s in l))
 ncols = ((tty_width() - len(prefix)) / (clen + 2))
 if (ncols <= 1):
  ncols = 1
  clen = 0
 cols = []
 while (len(l) % ncols):
  l.append('')
 rows = (len(l) / ncols)
 for s in range(0, len(l), rows):
  cols.append(l[s:(s + rows)])
 out = ''
 for row in zip(*cols):
  out += ((prefix + ''.join((('%-*s' % ((clen + 2), s)) for s in row))) + '\n')
 return out

def columnate ( l , prefix ) : if ( not l ) : return '' l = l [ : ] clen = max ( ( len ( s ) for s in l ) ) ncols = ( ( tty_width ( ) - len ( prefix ) ) / ( clen + 2 ) ) if ( ncols <= 1 ) : ncols = 1 clen = 0 cols = [ ] while ( len ( l ) % ncols ) : l . append ( '' ) rows = ( len ( l ) / ncols ) for s in range ( 0 , len ( l ) , rows ) : cols . append ( l [ s : ( s + rows ) ] ) out = '' for row in zip ( * cols ) : out += ( ( prefix + '' . join ( ( ( '%-*s' % ( ( clen + 2 ) , s ) ) for s in row ) ) ) + '\n' ) return out
format elements of l in columns with prefix leading each line .