[Tutor] Iterate Suggestion
bob gailer
bgailer at gmail.com
Mon Apr 16 04:54:43 CEST 2012
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
print '\n'.join(' '.join(mylist[i:i+3]) for i in range(0,len(mylist),3))
--
Bob Gailer
919-636-4239
Chapel Hill NC
More information about the Tutor
mailing list