[Tutor] Iterate Suggestion

bob gailer bgailer at gmail.com
Mon Apr 16 15:15:06 CEST 2012


On 4/15/2012 10:54 PM, bob gailer wrote:
> On 4/14/2012 11:27 AM, Tom Tucker wrote:
>>
>> Hello all.  Any suggestions how I could easily iterate over a list 
>> and print the output 3 across (when possible)?  One method I was 
>> considering was removing the recently printed item from the list, 
>> checking list length, etc.  Based on the remaining length of the list 
>> I would then print X across. Yah? Is their and easier approach I 
>> might be overlooking?
>>
>>
>> For example...
>>
>> mylist = ['serverA', 'serverB', 'serverC', 'serverD',' serverE', 
>> 'serverF', 'serverG']
>>
>>
>> Desired Output
>> ============
>> serverA  serverB  serverC
>> serverD  serverE  serverF
>> serverG
>
I amend my prior solution to add a constant for the # of columns

COLS = 3
> print '\n'.join(' '.join(mylist[i:i+COLS]) for i in 
> range(0,len(mylist),COLS))
>


-- 
Bob Gailer
919-636-4239
Chapel Hill NC



More information about the Tutor mailing list