Working with itertools

Itertools includes functions to work with iterables; it is inspired by a functional programming language such as Haskell. They promise to be memory-efficient and very fast.

Getting ready

There are a lot of functions available in Itertools; we will go through some of them as we work it out through examples. A link to the full list of functions has been provided.

How to do it…

Let's proceed to see a set of Python scripts used to demonstrate the usage of itertools:

# Load libraries
from itertools import chain,compress,combinations,count,izip,islice

# 1.Chain example, where different iterables can be combined together.
a = [1,2,3]
b = ['a','b','c']
print list(chain(a,b)) # prints [1, 2, 3, 'a', 'b', 'c']

# 2.Compress example, a data selector, where the data in the first iterator
#  is selected based on the second iterator.
a = [1,2,3]
b = [1,0,1]
print list(compress(a,b)) # prints [1, 3]

# 3.From a given list, return n length sub sequences.
a = [1,2,3,4]
print list(combinations(a,2)) # prints [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]

# 4.A counter which produces infinite consequent integers, given a start integer,
a = range(5)
b = izip(count(1),a)
for element in b:
    print element

# 5.	Extract an iterator from another iterator, 
# let us say we want an iterator which only returns every 
# alternate elements from the input iterator
a = range(100)
b = islice(a,0,100,2)
print list(b)

How it works…

Step 1 is pretty straightforward, where two iterables are combined using chain(). A point to note is that chain() is not realized till it's actually called. Check the following command line:

>>> chain(a,b)
<itertools.chain object at 0x060DD0D0>

Calling chain(a,b) returns the chain object. However, when we run the following command, the actual output is produced:

>>> list(chain(a,b))
[1, 2, 3, 'a', 'b', 'c']

Step 2 describes compress. In this example, elements of a are selected based on elements in b. You can see that in b, the second value is zero and hence, the second value in a is also not selected.

Step 3 does simple mathematical combinations. We have an input list, a, and want the elements of a in combinations of two.

Step 4 explains a counter object, which can serve as an infinite resource of a sequence number given a start number. Running the code, we will get the following output:

(1, 0)
(2, 1)
(3, 2)
(4, 3)
(5, 4)

You can see that we used izip here. (Zip and izip have been covered in previous sections.) Our output is a tuple where the first element is provided by counter and second element is provided by our input list, a.

Step 5 details the islice operation; islice is the same as slice, which we covered in the previous section, except that islice is memory-efficient and does not realize the complete output unless called upon.

Refer to https://docs.python.org/2/library/itertools.html for a complete list of the itertools.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset