7

I'm really stuck on a basic question. I am trying to take a list of one item and divide it into a list of many items each with a charater length of 10. For example give a list with one item, ['111111111122222222223333333333'], the output would produce:

1111111111
2222222222
3333333333

I feel like this is super simple, but I'm stumped. I tried to create a function like this:

def parser(nub):    
    while len(nub) > 10:  
        for subnub in nub:  
            subnub = nub[::10]
            return(subnub)  
    else:  
        print('Done')

Obviously, this doesn't work. Any advice? Would using a string be easier than a list?

machine yearning
  • 9,889
  • 5
  • 38
  • 51
drbunsen
  • 10,139
  • 21
  • 66
  • 94
  • can you rephrase this please: `I am trying to iterate through a list of n length into sublists of 10 characters.` i don't get it. – mouad Jun 16 '11 at 13:06
  • @mouad edited for clarity, i hope that helped. – drbunsen Jun 16 '11 at 13:25
  • Edit title for spelling. Also, your string doesn't need to be inside of a list. Also, did I not already answer your question? (see below) – machine yearning Jun 16 '11 at 13:33
  • possible duplicate of [How do you split a list into evenly sized chunks in Python?](http://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks-in-python) – tzot Jun 17 '11 at 06:26

4 Answers4

9

A related question has been asked: Slicing a list into a list of sub-lists

For example, if your source list is:

the_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, ... ]

you can split it like:

split_list = [the_list[i:i+n] for i in range(0, len(the_list), n)]

assuming n is your sub-list length and the result would be:

[[1, 2, 3, ..., n], [n+1, n+2, n+3, ..., 2n], ...]

Then you can iterate through it like:

for sub_list in split_list:
    # Do something to the sub_list

The same thing goes for strings.

Here's a practical example:

>>> n = 2
>>> listo = [1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> split_list = [listo[i:i+n] for i in range(0, len(listo), n)]
>>> split_list
[[1, 2], [3, 4], [5, 6], [7, 8], [9]]

>>> listo = '123456789'
>>> split_list = [listo[i:i+n] for i in range(0, len(listo), n)]
>>> split_list
['12', '34', '56', '78', '9']
Community
  • 1
  • 1
machine yearning
  • 9,889
  • 5
  • 38
  • 51
1

Although this question has been posted after 4 years, but here is a another way to do this use textwrap module. From the document:

textwrap.wrap(text[, width[, ...]])

Wraps the single paragraph in text (a string) so every line is at most width characters long. Returns a list of output lines, without final newlines.

Optional keyword arguments correspond to the instance attributes of TextWrapper, documented below. width defaults to 70.

So we can do it like this:

>>> import textwrap
>>> myList = ['111111111122222222223333333333']

>>> [i for text in myList for i in textwrap.wrap(text, 10)]
['1111111111', '2222222222', '3333333333']

>>> for i in [i for text in myList for i in textwrap.wrap(text, 10)]:
...     print i
1111111111
2222222222
3333333333
>>> 
Community
  • 1
  • 1
Remi Guan
  • 21,506
  • 17
  • 64
  • 87
1

Use:

value = '111111111122222222223333333333'
n = 10
(value[i:i+n] for i in xrange(0, len(value), n))
Artsiom Rudzenka
  • 27,895
  • 4
  • 34
  • 52
0

Other ways to do it recursively:

Option 1: Recursive function

>>> def chunks(x, n=10):
...      if len(x) <= n:
...          return [x]
...      else:
...          return [x[:n]] + chunks(x.replace(x[:n], ''))
...
>>> seq = ['111111111122222222223333333333']
>>> print chunks(seq[0])
['1111111111', '2222222222', '3333333333']

Option 2: Recursive lambda

>>> n = 10
>>> chunks = lambda x: [x] if len(x) <= n else [x[:n]] + chunks(x.replace(x[:n], ''))
>>> print chunks(seq[0])
['1111111111', '2222222222', '3333333333']
Aziz Alto
  • 19,057
  • 5
  • 77
  • 60