0

My list is ,

['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

want to print every n elements of this list. Means in each iteration want to print N*elements from that list.

I tried ,

If N is 15.

>>> a=list(string.ascii_lowercase)
>>> for i in range(len(a)-1):
    print a[i:i+15]

It prints

['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o']
['b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p']
['c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q']
['d', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r']
['e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's']
['f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't']
['g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u']
['h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v']
['i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w']
['j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x']
['k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y']
['l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
['m', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
['n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
['o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
['p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
['q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
['r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
['s', 't', 'u', 'v', 'w', 'x', 'y', 'z']
['t', 'u', 'v', 'w', 'x', 'y', 'z']
['u', 'v', 'w', 'x', 'y', 'z']
['v', 'w', 'x', 'y', 'z']
['w', 'x', 'y', 'z']
['x', 'y', 'z']
['y', 'z']

What I want is ,

if N=15

['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o']
['p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

If N=5 ,

['a', 'b', 'c', 'd', 'e']
['f', 'g', 'h', 'i', 'j']
['k', 'l', 'm', 'n', 'o']
['p', 'q', 'r', 's', 't']
['u', 'v', 'w', 'x', 'y']
['z']

How to do this ?

oyks
  • 11
  • 6

1 Answers1

6

Just use a step on the range:

for i in range(0, len(a), 15):
    print a[i:i+15]

Also note that I changed your len(a)-1 to len(a). Ranges in Python are half-open, meaning you give it the first value, and one past the last value. So, to count the first 26 numbers, from 0 to 25, you use range(26), not range(25).


There may be better ways to write this; for example, with a grouper or chunker function like the one in the itertools recipes:

for group in grouper(a, 15, fillvalue=None):
    print filter(None, group)

But the step is the smallest change from what you have.

abarnert
  • 354,177
  • 51
  • 601
  • 671
  • If N=5 it doesnt print the last z – vks Aug 12 '14 at 05:54
  • @vks: That's because the OP explicitly wrote `len(a)-1` instead of `len(a)`, and I assumed that was for a reason… but maybe I assumed wrong. I'll edit it, as long as I can figure out how to explain it well enough that if the `-1` was there for a good reason the OP will understand why I changed it and can decide not to. – abarnert Aug 12 '14 at 06:02
  • Removed the downvote.its working fine now.i actually liked d simplicity :) – vks Aug 12 '14 at 06:30