Don't forget to also checkout my second blog containing articles to all other related ICT topics!!

Thursday, May 3, 2012

Itertools takewhile method explained

Itertools takewhile makes an iterator that returns elements from the iterable as long as the predicate is true.
import itertools
#we first produce a sequence of numbers stepped by 2
#next we take elements of this sequence while the element is smaller than 11
print list(itertools.takewhile(lambda x: x < 11, itertools.count(0,2)))

[0, 2, 4, 6, 8, 10]

No comments:

Post a Comment