Welcome, guest | Sign In | My Account | Store | Cart

The code below takes a string and returns a list containing the n-sized pieces of the string. For example: splitCount('aabbccdd', 2) => ['aa', 'bb', 'cc', 'dd']

Python, 2 lines
def splitCount(s, count):
     return [''.join(x) for x in zip(*[list(s[z::count]) for z in range(count)])]

This is useful if you know that data is in a certain format. I use it to parse dates that are not delimited like: 060506 (often found in filenames, etc).

Anyways it seems like kind of a basic operation on strings.

It seems like there should be a more pythonic way to do this, but I can't think of one.


Ian Bicking 16 years, 2 months ago  # | flag

For sequences... I think this might be better, and works for any sequences that support slicing and len() (thus including lists):

def split_len(seq, length):
    return [seq[i:i+length] for i in range(0, len(seq), length)]
Marco Ippolito 8 years, 8 months ago  # | flag

Ian, they are not equivalent:


seq='f09f9989x'; length=2; [seq[i:i+length] for i in range(0, len(seq), length)]
['f0', '9f', '99', '89', 'x']

== Vs ==


seq='f09f9989x'; length=2; [''.join(x) for x in zip(*[list(seq[z::length]) for z in range(length)])]
['f0', '9f', '99', '89']

(notice the difference in results)

Or, similarly:

seq='f09f9989x'; length=2; map(''.join, zip(*[iter(seq)]*length))
['f0', '9f', '99', '89']
Created by Matt Wallace on Tue, 6 Jun 2006 (PSF)
Python recipes (4591)
Matt Wallace's recipes (2)

Required Modules

  • (none specified)

Other Information and Tasks