{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "##
\n",
"for item in container:\n",
" print(item)\n",
"\n",
"The lists, strings and tuples are iterable objects. Dictionaries and files are also iterable."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Iteration is the process of accessing and returning one item at a time from a container."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Examples:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2\n",
"5\n",
"1\n"
]
}
],
"source": [
"L = [2, 5, 1]#list\n",
"for k in L:\n",
" print(k)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"s\n",
"t\n",
"r\n",
"i\n",
"n\n",
"g\n"
]
}
],
"source": [
"S = \"string\"\n",
"for c in S:\n",
" print(c)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4\n",
"9\n",
"7\n"
]
}
],
"source": [
"T=(4, 9, 7)#tuple\n",
"for t in T:\n",
" print(t)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The iteration over a dictionary `d` returns its keys:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"lst\n",
"today\n",
"nr\n"
]
}
],
"source": [
"d={'lst': [4,7,1], 'today': 'monday', 'nr': 10}\n",
"\n",
"for item in d:\n",
" print(item)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"An iteration over a file object returns the file's lines:"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"this is the first line\n",
"\n",
"second line\n",
"\n",
"final line\n",
"\n"
]
}
],
"source": [
"f = open(\"toyfile.txt\")# f is a file object i.e. a container for the file's lines \n",
"for line in f:\n",
" print(line)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The above examples are built-in iterable objects in Python. \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Iterators"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" Intuitively, an iterator is an object that produces a stream of items, according to a prescribed protocol. \n",
"\n",
"From the programming language point of view an iterator is an object of a class that has the methods `__iter__`, and `__next__`. By the iterator protocol these methods act as follows:\n",
"\n",
"The `__iter__` method returns the iterator object itself, and the `__next__` method returns the next item of the stream.\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"How does a container, that is an iterable object, relate with an iterator?\n",
"\n",
"The built-in function `iter` takes a container and returns an iterator."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In all the above examples, `for item in container` launches the iterator protocol.\n",
" The `for` statement calls `iter()` on the container object. \n",
"\n",
" - `it=iter(container)` is an iterator object and by calling `next(it)`\n",
" are accessed the container's items, one at a time. \n",
"\n",
" - When there are no more items, `next(it)`\n",
" raises a `StopIteration` exception, which forces the `for` loop to terminate.\n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let us explicitly call `iter()` on the list, L, and file object f, defined above:\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2 5 1\n"
]
}
],
"source": [
"it_list=iter(L)\n",
"print (next(it_list), next(it_list), next(it_list))"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"ename": "StopIteration",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mStopIteration\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m