Iterator chaining: Basic Syntax

Let's start with two iterables, in this case, two lists of cookie types.

box1 = iter(["chocolate chip", "oatmeal raisin", "sugar"])
box2 = iter(["shortbread", "gingerbread"])
We're going to consolidate these two iterables into one iterable using iterator chaining, so we can treat the two sequences as though they were one long sequence. In Python, this is done using the itertools.chain() functionality.

cookiebox =chain(box1, box2)
In Coconut this is simplified by using the :: operator.

cookiebox = box1 :: box2
The :: command tells Coconut to link the two iterators.
The chain command also can work to link to iterables into one big iterator. Try using the chaining command to link two range functions together! If you would like, you an also convert to a list and print to check the results.

Show Solution:


Next
Previous