Pretty print Python Lists -


i have written cli utility in python , have list 1:m list-items of paths like: imgggg

i want display list in more readable manner like:

[ (1, '/path/here'), ('path/here/again'),  (2, 'a/different/path/here'), ('another/path/here') ] 

or in table-like format (e.g.):

1    /path/here                path/here/again 2    a/different/path/here     another/path/here 

note, list have 20 or more list-items.

thanks!

to start:

>>> mylist = [ ... (1, '/path/here', 'path/here/again'), ... (2, 'a/different/path/here', 'another/path/here') ... ] 

play join(), map(), , str(), along python 3's nice print() function:

>>> print(*('\t'.join(map(str, item)) item in mylist), sep='\n') 1       /path/here      path/here/again 2       a/different/path/here   another/path/here 

or try string formatting instead of join() , map():

>>> print(*(str(col) + '\t' + (len(item)*'{}').format(*(i.ljust(25) in item)) col,*item in mylist), sep='\n') 1       /path/here               path/here/again 2       a/different/path/here    another/path/here 

you pprint module.


Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -