Say I have a list
food_list = ['apple', 'pear', 'tomato', 'bean', 'carrot', 'grape']
How can I print the list in rows containing 4 columns, so it will look like this:
apple pear tomato bean carrot grape
food_list = ['apple', 'pear', 'tomato', 'bean', 'carrot', 'grape'] for i in xrange(0, len(food_list), 4): print '\t'.join(food_list[i:i+4])
Try with this
food_list = ['apple', 'pear', 'tomato', 'bean', 'carrot', 'grape'] size = 4 g = (food_list[i:i+size] for i in xrange(0, len(food_list), size)) for i in g: print i
food_list = ['apple', 'pear', 'tomato', 'bean', 'carrot', 'grape'] index = 0 for each_food in food_list: if index < 3: print each_food, index += 1 else: print each_food index = 0
Source: https://habr.com/ru/post/1753924/More articles:ссылка на вектор:: фронт работает, но вектор:: begin не - c++Аргументы исчезают из словаря при передаче функции - pythonGet identification value from a set of radio buttons - javascriptxor all the data in the package - c ++Python and HTML table - pythonДолжен ли я использовать WCF или веб-сервис ASMX? - asp.netБуфер слишком мал при копировании строки с помощью wcsncpy_s - c++Захват уникального номера сборки Maven SNAPSHOT - maven-2Should you encrypt data in app.config and web.config in this situation? - securityhow to create a download site using maven - maven-2All Articles