Iterables

Objects that do not have the __iter__ or next function but can be used to create an iterator are iterables. The built-in __iter__ function takes a sequential object as input and returns an iterator. Then, another built-in function, next, takes the iterator and returns the iterator's elements in every invocation. Lists and tuples are not iterators but can be used to create iterators using the next function. The following code snippet demonstrates using a list as an iterator:

mylist = [1,2,3,4]mylist_iter = iter(mylist)print(type(mylist))print(type(mylist_iter))<class 'list'><class 'list_iterator'>

Notice that mylist is of type list while mylist_iter, which is created by the iter function, is of type list_iterator.

Get Practical Time-Series Analysis now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.