Manoj Govindan Manoj Govindan - 3 months ago 7
Python Question

Is there a generator version of `string.split()` in Python?

returns a list instance. Is there a version that returns a generator instead? Are there any reasons against having a generator version?

Answer

It is highly probably that re.finditer(link) uses fairly minimal memory overhead.

def split_iter(string):
    return (x.group(0) for x in re.finditer(r"[A-Za-z']+", string))

Demo:

>>> list( split_iter("A programmer's RegEx test.") )
['A', "programmer's", 'RegEx', 'test']

edit: I have just confirmed that this takes constant memory in python 3.2.1, assuming my testing methodology was correct. I created a string of very large size (1GB or so), then iterated through the iterable with a for loop (NOT a list comprehension, which would have generated extra memory). This did not result in a noticeable growth of memory (that is, if there was a growth in memory, it was far far less than the 1GB string).