How to print a list more nicely?

前端 未结 22 1263
北荒
北荒 2020-12-01 09:34

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 b

22条回答
  •  南方客
    南方客 (楼主)
    2020-12-01 10:07

    For Python >=3.6, slight update to @JoshuaZastrow's answer using f-strings and adding clear method to adjust columns

    cols = 5
    [print(f'{key:20}', end='\t') if (idx + 1) % cols else print(f'{key}') for idx, key in enumerate(list_variable)]
    

    or

    cols = 5
    for idx, key in enumerate(list_variable):
        if (idx + 1) % cols:
            print(f'{key:20}', end='\t')
        else:
            print(f'{key}')
    

提交回复
热议问题