Generating problems

For a recent project I used groupby from itertools.
I had to check the label of the first object that the groupby returns.
My first guess was:

from itertools import groupby
groups = groupby("00001110000111")
print(groups[0][0])

that gave

TypeError: 'itertools.groupby' object has no attribute '__getitem__'

my second guess was using next:

from itertools import groupby
groups = groupby("0001110000111")
print(next(groups)[0])

And worked! The first element is now gone without return and all next accesses of groups refer to the remaining elements.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s