-2

Possible Duplicate:
Iterate over a python sequence in multiples of n?

How to list elements like: ['abcd', 'efghi'] ?

Using the next:

test = map(chr, range(97, 123))
for i in test:
if len(el) == 2:
    break
while len(i) != 4:
    i = i + i
el.append(i)

it shows: ['aaaa', 'bbbb'] and it's correct in this case

Community
  • 1
  • 1
ouea
  • 203
  • 2
  • 6

1 Answers1

3

Are you looking for list comprehensions?

>>> [''.join(test[i:i+4]) for i in range(0, len(test), 4)]
['abcd', 'efgh', 'ijkl', 'mnop', 'qrst', 'uvwx', 'yz']
Roman Bodnarchuk
  • 29,461
  • 12
  • 59
  • 75