[docs] Example for itertools.groupby()

Jesse Weinstein jesse at wefu.org
Wed Oct 13 10:55:16 CEST 2010

I noticed that itertools.groupby() lacks an example of it's use.  (as
shown in the table at the top of:
http://docs.python.org/library/itertools.html ).  Here's one quick one I
hacked up:

[''.join([z for z in y]) for (x,y) in itertools.groupby("What's up,
brown cow?",str.isalpha) if x]

will print the words in the string.

In a form more matching the existing examples:

itertools.groupby("Hello, world!",str.isalpha) -> 
(True, "Hello"), (False, ", "), (True, "world"), (False, "!")

Feel free to use it if you wish.  I renounce any copyright interests I
may have in it (if it's even long enough to avoid "de minimis").

Jesse Weinstein

More information about the docs mailing list