First Exposure: Python Iterators

Let's start by making a list of cookie types, converting it into an iterator, and entering it into our Coconut interpreter:

cookiebox = iter(["chocolate chip", "oatmeal raisin", "sugar"])
This is how to take an iterable object, such as a list, and turn it into an iterator in Python. As we see, this, and the other basic iterator functionality from Python remains the same. Try entering the following command into the interpreter a few times to iterate through the cookiebox.

cookiebox.next()
Now let's try combining this with Coconut's syntax. Let's start by defining x as the following iterator:

x = iter([1, 2, 3])
Using Coconut's pipeline syntax, convert the iterator back into a list, and print the result. This should only take one line of code.

Show Solution:


Next
Previous