


The following example extracts every third character from the alphabet. The next function in the collection is itertools.islice() which returns an iterator for looping over slices in some ordered fashion. While this might not matter for small lists, it makes a difference when processing a large number of items. Well, itertools.ifilter() returns an iterator while filter() returns a list. So what is the difference between ifilter() and filter()? Python has a global function called filter() which also filters items in a list through a predicate function. Difference between ifilter() and filter() print 'randoms div 3: ', list(itertools.ifilter(lambda n : not n % 3, ))Ģ.1. Or some random numbers divisible by 3? You get the picture. Select a few random even integers? print 'random evens: ', list(itertools.ifilter(lambda n : n % 2 = 0, )) print list(itertools.ifilter(lambda c : c > 'e', 'abcdefg')) In this article, we present a few examples of ifilter(), islice(), imap() and izip().įirst off, we have itertools.ifilter() which filters an iterable or a list for items for which a predicate function returns true.Ĭonsider this simple example which selects all characters with a numeric value greater than ‘e’. We have covered count(), cycle() and chain() in the first part of this series, and compress(), dropwhile(), and groupby() in the second part. Python provides the itertools package which provides convenience functions for many common iterator operations. Difference between Array Slice Syntax and islice()

