lemontree
lemontree

Reputation: 99

How to print from itertools count object?

I'm using itertools count method to keep track of how many instances of a class have been created. My simplified code looks something like this:

from itertools import count
a = count(1)
a.next()
a.next()
print a

count(3)

I want to print just the "3", without count. Sounds simple right?

Upvotes: 1

Views: 2318

Answers (1)

Joran Beasley
Joran Beasley

Reputation: 113988

if it sounds simple ... it probably is

from itertools import count
a = count(1)

next(a)
next(a)
print next(a)

you can also use itertools.islice to skip parts of an iterator

from itertools import count,islice
a = count(1)
for item in islice(a,2,4):
    print item

Upvotes: 1

Related Questions