pythonprinting

How to print a list more nicely?


This is similar to How to print a list in Python “nicely”, but I would like to print the list even more nicely -- without the brackets and apostrophes and commas, and even better in columns.

foolist = ['exiv2-devel', 'mingw-libs', 'tcltk-demos', 'fcgi', 'netcdf', 
    'pdcurses-devel',     'msvcrt', 'gdal-grass', 'iconv', 'qgis-devel', 
    'qgis1.1', 'php_mapscript']

evenNicerPrint(foolist)

Desired result:

exiv2-devel       msvcrt        
mingw-libs        gdal-grass    
tcltk-demos       iconv         
fcgi              qgis-devel    
netcdf            qgis1.1       
pdcurses-devel    php_mapscript 

thanks!


Solution

  • Although not designed for it, the standard-library module in Python 3 cmd has a utility for printing a list of strings in multiple columns

    import cmd
    cli = cmd.Cmd()
    cli.columnize(foolist, displaywidth=40)
    

    Output:

    exiv2-devel     msvcrt       
    mingw-libs      gdal-grass   
    tcltk-demos     iconv        
    fcgi            qgis-devel   
    netcdf          qgis1.1      
    pdcurses-devel  php_mapscript
    

    You even then have the option of specifying the output location, with cmd.Cmd(stdout=my_stream)