Print 5 items in a row on separate lines for a list?

前端 未结 11 922
耶瑟儿~
耶瑟儿~ 2021-02-07 19:15

I have a list of unknown number of items, let\'s say 26. let\'s say

list=[\'a\',\'b\',\'c\',\'d\',\'e\',\'f\',\'g\',\'h\',
\'i\',\'j\',\'k\',\'l\',\'m\',\'n\',\'         


        
11条回答
  •  深忆病人
    2021-02-07 19:36

    You can something easier like below , break your list into sub-list , also this seems more Pythonic . Then print it how ever u want . Also don't use list as it's a keyword(not recommended)

    sub_list1=[list1[x:x+5] for x in xrange(0, len(list1), 5)]
    for each in sub_list1:
        print( ''.join(each))
    

提交回复
热议问题