{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [], "authorship_tag": "ABX9TyPm5glslSsJ2NaiEWd7Q0El", "include_colab_link": true }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "\"Open" ] }, { "cell_type": "markdown", "source": [ "# **What Are Iterables and Iterators in Python?**\n", "In Python some objects can be looped over, broadly these objects can be divided into two groups: iterator and iterables. An iterable is any object that can be looped over, such as lists, tuples, dictionaries, and strings whereas iterator is an object that enables a programmer to traverse through all the elements of a collection, one element at a time in a forward direction only. Example of an iterator generator.\n", "\n", "Understanding Iterables:\n", "\n", "An iterable is anything you can iterate over. In Python, objects like lists, tuples, sets, and dictionaries fall into this category. When you use a for loop, you are implicitly using an iterable. We can move back and forth in iterables only. All the iterables have only the dunder iter method with them." ], "metadata": { "id": "MW-UT6LxMg4G" } }, { "cell_type": "code", "execution_count": 9, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "5VOYjPVaL_W-", "outputId": "689100d9-1863-472f-f6d7-1d3d77c07dfc" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "1\n", "2\n", "3\n", "4\n", "[1, 2, 3, 4]\n", "[1, 2, 3, 4]\n", "[1, 2, 3, 4]\n", "[1, 2, 3, 4]\n", "[1, 2, 3, 4]\n", "1\n", "2\n", "3\n", "4\n", "1\n", "2\n", "3\n", "4\n", "Index 0: Geeks\n", "Index 1: for\n", "Index 2: Geeks\n", "[(0, 'Geeks'), (1, 'for'), (2, 'Geeks')]\n", "1 geeks\n", "2 for\n", "3 geeks\n", "(0, 'Geeks')\n", "(1, 'for')\n", "0 - a : 10\n", "1 - b : 20\n", "2 - c : 30\n", "Index 0: p\n", "Index 1: y\n", "Index 2: t\n", "Index 3: h\n", "Index 4: o\n", "Index 5: n\n", "Index 0: apple\n", "Index 1: banana\n", "Index 2: cherry\n", "1\n", "2\n", "3\n", "4\n", "1\n", "2\n", "3\n", "4\n", "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z A\n", "B\n", "C\n", "1\n", "2\n", "3\n", "True\n", "orange\n", "green\n", "black\n", "orange\n", "green\n", "black\n" ] } ], "source": [ "my_list = [1, 2, 3, 4]\n", "for item in my_list:\n", " print(item)\n", "\n", "class Counter:\n", " def __init__(self, start, end):\n", " self.start = start\n", " self.end = end\n", "\n", " def __iter__(self):\n", " return self\n", "\n", " def __next__(self):\n", " if self.start > self.end:\n", " raise StopIteration\n", " else:\n", " self.start += 1\n", " return self.start - 1\n", "\n", "counter = Counter(1, 5)\n", "for number in counter:\n", "\n", " my_list = [1,2,3,4]\n", " my_iterator_list = my_list.__iter__()\n", " print(my_list)\n", "\n", "my_list = [1,2,3,4]\n", "my_iterator_list = iter(my_list)\n", "\n", "print(next(my_iterator_list))\n", "print(next(my_iterator_list))\n", "print(next(my_iterator_list))\n", "print(next(my_iterator_list))\n", "\n", "\n", "my_list = [1,2,3,4]\n", "my_iterator_list = iter(my_list)\n", "\n", "while True:\n", " try:\n", " looped = next(my_iterator_list)\n", " print(looped)\n", " except StopIteration:\n", " break\n", "\n", "a = [\"Geeks\", \"for\", \"Geeks\"]\n", "\n", "# Iterating list using enumerate to get both index and element\n", "for i, name in enumerate(a):\n", " print(f\"Index {i}: {name}\")\n", "\n", "# Converting to a list of tuples\n", "print(list(enumerate(a)))\n", "\n", "a = [\"geeks\", \"for\", \"geeks\"]\n", "\n", "#Looping through the list using enumerate\n", "# starting the index from 1\n", "for index, x in enumerate(a, start=1):\n", " print(index, x)\n", "\n", "a = ['Geeks', 'for', 'Geeks']\n", "\n", "# Creating an enumerate object from the list 'a'\n", "b = enumerate(a)\n", "\n", "# This retrieves the first index-element pair\n", "nxt_val = next(b)\n", "print(nxt_val)\n", "\n", "# This retrieves the second index-element pair\n", "nxt_val = next(b)\n", "print(nxt_val)\n", "\n", "d = {\"a\": 10, \"b\": 20, \"c\": 30}\n", "\n", "# Enumerating through dictionary items\n", "for index, (key, value) in enumerate(d.items()):\n", " print(index, \"-\", key, \":\", value)\n", "\n", "\n", "s = \"python\"\n", "for i, ch in enumerate(s):\n", " print(f\"Index {i}: {ch}\")\n", "\n", "s = {\"apple\", \"banana\", \"cherry\"}\n", "for i, fruit in enumerate(s):\n", " print(f\"Index {i}: {fruit}\")\n", "\n", "from typing import TypedDict\n", "#from langgraph.graph import StateGraph, START, END\n", "\n", "\n", "class State(TypedDict):\n", " topic: str\n", " joke: str\n", "\n", "\n", "def refine_topic(state: State):\n", " return {\"topic\": state[\"topic\"] + \" and cats\"}\n", "\n", "\n", "def generate_joke(state: State):\n", " return {\"joke\": f\"This is a joke about {state['topic']}\"}\n", "'''\n", "graph = (\n", " StateGraph(State)\n", " .add_node(refine_topic)\n", " .add_node(generate_joke)\n", " .add_edge(START, \"refine_topic\")\n", " .add_edge(\"refine_topic\", \"generate_joke\")\n", " .add_edge(\"generate_joke\", END)\n", " .compile()\n", ")\n", "\n", "for chunk in graph.stream(\n", " {\"topic\": \"ice cream\"},\n", " stream_mode=\"updates\",\n", "):\n", " print(chunk)\n", "'''\n", "nums = [1, 2, 3, 4]\n", "obj = iter(nums)\n", "print(next(obj))\n", "print(next(obj))\n", "print(next(obj))\n", "print(next(obj))\n", "\n", "def nums():\n", " for i in range(1, 5):\n", " yield i\n", "\n", "obj = nums()\n", "print(next(obj))\n", "print(next(obj))\n", "print(next(obj))\n", "print(next(obj))\n", "\n", "class Alphabets:\n", "\n", " def __iter__(self):\n", " self.val = 65\n", " return self\n", "\n", " def __next__(self):\n", " if self.val > 90:\n", " raise StopIteration\n", " temp = self.val\n", " self.val += 1\n", " return chr(temp)\n", "\n", "my_letters = Alphabets()\n", "my_iterator = iter(my_letters)\n", "for letter in my_iterator:\n", " print(letter, end = \" \")\n", "\n", "def Alphabets():\n", "\n", " for i in range(65, 91):\n", " yield chr(i)\n", "\n", "\n", "my_letters = Alphabets()\n", "\n", "for letter in my_letters:\n", " print(letter, end=\" \")\n", "\n", "li = [\"A\", \"B\", \"C\", \"D\"]\n", "li_iter = iter(li)\n", "print(next(li_iter))\n", "print(next(li_iter))\n", "print(next(li_iter))\n", "\n", "def gener():\n", " num = 1\n", " while True:\n", " yield num\n", " num += 1\n", "\n", "obj = gener()\n", "print(next(obj))\n", "print(next(obj))\n", "print(next(obj))\n", "\n", "from collections.abc import Generator, Iterator\n", "\n", "print(issubclass(Generator, Iterator))\n", "\n", "List = [\"orange\", \"green\", \"black\"]\n", "list_iter = iter(List)\n", "print(next(list_iter))\n", "print(next(list_iter))\n", "print(next(list_iter))\n", "\n", "def gener():\n", " List = [\"orange\", \"green\", \"black\"]\n", " for item in List:\n", " yield item\n", "\n", "iter_obj = gener()\n", "print(next(iter_obj))\n", "print(next(iter_obj))\n", "print(next(iter_obj))\n", "\n" ] }, { "cell_type": "markdown", "source": [ "# **Creating Custom Iterators:**\n", "You can create user defined iterator objects by defining the __iter__() and __next__() methods in a class." ], "metadata": { "id": "a5j9cXy2Mh2V" } }, { "cell_type": "code", "source": [ "def abcd():\n", "\n", " for i in range(97, 101):\n", " yield chr(i)\n", "\n", "obj = abcd()\n", "print(next(obj))\n", "print(next(obj))\n", "print(next(obj))\n", "print(next(obj))\n", "\n", "class Multiples:\n", "\n", " def __iter__(self):\n", " self.val = 1\n", " return self\n", "\n", " def __next__(self):\n", " temp = self.val\n", " self.val += 1\n", " return temp*5\n", "\n", "multiples5 = Multiples()\n", "obj = iter(multiples5)\n", "\n", "print(next(obj))\n", "print(next(obj))\n", "print(next(obj))\n", "\n", "def Multiples():\n", " i = 1\n", " while True:\n", " yield i*5\n", " i += 1\n", "\n", "multiples5 = Multiples()\n", "obj = multiples5\n", "print(next(obj))\n", "print(next(obj))\n", "print(next(obj))\n", "\n", "\n", "# Define a custom iterator class named MyIterator.\n", "class MyIterator:\n", " # Initialize the iterator with data and set the index to 0.\n", " def __init__(self, data):\n", " self.data = data\n", " self.index = 0\n", "\n", " # Implement the __iter__ method, which returns the iterator object itself.\n", " def __iter__(self):\n", " return self\n", "\n", " # Implement the __next__ method, which is called to retrieve the next item from the iterator.\n", " def __next__(self):\n", " # Check if the index has reached the end of the data.\n", " if self.index >= len(self.data):\n", " # Raise StopIteration to signal the end of the iteration.\n", " raise StopIteration\n", " # Get the value at the current index.\n", " value = self.data[self.index]\n", " # Increment the index for the next iteration.\n", " self.index += 1\n", " # Return the value for the current iteration.\n", " return value\n", "\n", "my_list = [1, 2, 3, 4, 5]\n", "my_iterator = MyIterator(my_list)\n", "\n", "for item in my_iterator:\n", " print(item)\n", "\n", "\n", "# Define a generator function named my_generator.\n", "def my_generator(data):\n", " # Iterate through each item in the 'data' sequence.\n", " for item in data:\n", " # Yield the current item, effectively producing it as the next value in the generator.\n", " yield item\n", "\n", "\n", "my_list = [1, 2, 3, 4, 5]\n", "my_gen = my_generator(my_list)\n", "\n", "for item in my_gen:\n", " print(item)\n", "\n", "\n", "cities = [\"Paris\", \"Berlin\", \"Hamburg\",\n", " \"Frankfurt\", \"London\", \"Vienna\",\n", " \"Amsterdam\", \"Den Haag\"]\n", "for location in cities:\n", " print(\"location: \" + location)\n", "\n", "expertises = [\"Python Beginner\",\n", " \"Python Intermediate\",\n", " \"Python Proficient\",\n", " \"Python Advanced\"]\n", "expertises_iterator = iter(expertises)\n", "print(\"Calling 'next' for the first time: \", next(expertises_iterator))\n", "print(\"Calling 'next' for the second time: \", next(expertises_iterator))\n", "\n", "other_cities = [\"Strasbourg\", \"Freiburg\", \"Stuttgart\",\n", " \"Vienna / Wien\", \"Hannover\", \"Berlin\",\n", " \"Zurich\"]\n", "\n", "city_iterator = iter(other_cities)\n", "while city_iterator:\n", " try:\n", " city = next(city_iterator)\n", " print(city)\n", " except StopIteration:\n", " break\n", "\n", "capitals = {\n", " \"France\":\"Paris\",\n", " \"Netherlands\":\"Amsterdam\",\n", " \"Germany\":\"Berlin\",\n", " \"Switzerland\":\"Bern\",\n", " \"Austria\":\"Vienna\"}\n", "\n", "for country in capitals:\n", " print(\"The capital city of \" + country + \" is \" + capitals[country])\n", "\n", "\n", "class Cycle(object):\n", "\n", " def __init__(self, iterable):\n", " self.iterable = iterable\n", " self.iter_obj = iter(iterable)\n", "\n", " def __iter__(self):\n", " return self\n", "\n", " def __next__(self):\n", " while True:\n", " try:\n", " next_obj = next(self.iter_obj)\n", " return next_obj\n", " except StopIteration:\n", " self.iter_obj = iter(self.iterable)\n", "\n", "\n", "x = Cycle(\"abc\")\n", "\n", "for i in range(10):\n", " print(next(x), end=\", \")\n", "\n", "def count(firstval=0, step=1):\n", " x = firstval\n", " while True:\n", " yield x\n", " x += step\n", "\n", "counter = count() # count will start with 0\n", "for i in range(10):\n", " print(next(counter), end=\", \")\n", "\n", "start_value = 2.1\n", "stop_value = 0.3\n", "print(\"\\nNew counter:\")\n", "counter = count(start_value, stop_value)\n", "for i in range(10):\n", " new_value = next(counter)\n", " print(f\"{new_value:2.2f}\", end=\", \")\n", "\n", "def fibonacci(n):\n", " \"\"\" A generator for creating the Fibonacci numbers \"\"\"\n", " a, b, counter = 0, 1, 0\n", " while True:\n", " if (counter > n):\n", " return\n", " yield a\n", " a, b = b, a + b\n", " counter += 1\n", "f = fibonacci(5)\n", "for x in f:\n", " print(x, \" \", end=\"\") #\n", "print()\n", "\n", "def fibonacci():\n", " \"\"\"Generates an infinite sequence of Fibonacci numbers on demand\"\"\"\n", " a, b = 0, 1\n", " while True:\n", " yield a\n", " a, b = b, a + b\n", "\n", "f = fibonacci()\n", "\n", "counter = 0\n", "for x in f:\n", " print(x, \" \", end=\"\")\n", " counter += 1\n", " if (counter > 10):\n", " break\n", "print()\n", "\n", "def simple_coroutine():\n", " print(\"coroutine has been started!\")\n", " while True:\n", " x = yield \"foo\"\n", " print(\"coroutine received: \", x)\n", "\n", "\n", "cr = simple_coroutine()\n", "cr\n", "\n", "def count(firstval=0, step=1):\n", " counter = firstval\n", " while True:\n", " new_counter_val = yield counter\n", " if new_counter_val is None:\n", " counter += step\n", " else:\n", " counter = new_counter_val\n", "\n", "start_value = 2.1\n", "stop_value = 0.3\n", "counter = count(start_value, stop_value)\n", "for i in range(10):\n", " new_value = next(counter)\n", " print(f\"{new_value:2.2f}\", end=\", \")\n", "\n", "print(\"set current count value to another value:\")\n", "counter.send(100.5)\n", "for i in range(10):\n", " new_value = next(counter)\n", " print(f\"{new_value:2.2f}\", end=\", \")\n", "\n" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "0gobuwF2Mh_O", "outputId": "45b0f1ec-d870-4327-f7bf-d6afb3bf3a33" }, "execution_count": 11, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "a\n", "b\n", "c\n", "d\n", "5\n", "10\n", "15\n", "5\n", "10\n", "15\n", "1\n", "2\n", "3\n", "4\n", "5\n", "1\n", "2\n", "3\n", "4\n", "5\n", "location: Paris\n", "location: Berlin\n", "location: Hamburg\n", "location: Frankfurt\n", "location: London\n", "location: Vienna\n", "location: Amsterdam\n", "location: Den Haag\n", "Calling 'next' for the first time: Python Beginner\n", "Calling 'next' for the second time: Python Intermediate\n", "Strasbourg\n", "Freiburg\n", "Stuttgart\n", "Vienna / Wien\n", "Hannover\n", "Berlin\n", "Zurich\n", "The capital city of France is Paris\n", "The capital city of Netherlands is Amsterdam\n", "The capital city of Germany is Berlin\n", "The capital city of Switzerland is Bern\n", "The capital city of Austria is Vienna\n", "a, b, c, a, b, c, a, b, c, a, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, \n", "New counter:\n", "2.10, 2.40, 2.70, 3.00, 3.30, 3.60, 3.90, 4.20, 4.50, 4.80, 0 1 1 2 3 5 \n", "0 1 1 2 3 5 8 13 21 34 55 \n", "2.10, 2.40, 2.70, 3.00, 3.30, 3.60, 3.90, 4.20, 4.50, 4.80, set current count value to another value:\n", "100.80, 101.10, 101.40, 101.70, 102.00, 102.30, 102.60, 102.90, 103.20, 103.50, " ] } ] }, { "cell_type": "markdown", "source": [ "# **Understanding Iterables**\n", "In Python, an iterable is any object capable of returning its elements one at a time. It represents a collection of items that can be iterated upon. Examples of built-in iterables include lists, tuples, strings, and dictionaries." ], "metadata": { "id": "lai0JFZMMiH2" } }, { "cell_type": "code", "source": [ "from random import choice\n", "\n", "def song_generator(song_list):\n", " new_song = None\n", " while True:\n", " if new_song != None:\n", " if new_song not in song_list:\n", " song_list.append(new_song)\n", " new_song = yield new_song\n", " else:\n", " new_song = yield choice(song_list)\n", "\n", "songs = [\"Her Şeyi Yak - Sezen Aksu\",\n", " \"Bluesette - Toots Thielemans\",\n", " \"Six Marimbas - Steve Reich\",\n", " \"Riverside - Agnes Obel\",\n", " \"Not for Radio - Nas\",\n", " \"What's going on - Taste\",\n", " \"On Stream - Nils Petter Molvær\",\n", " \"La' Inta Habibi - Fayrouz\",\n", " \"Ik Leef Niet Meer Voor Jou - Marco Borsato\",\n", " \"Δέκα λεπτά - Αθηνά Ανδρεάδη\"]\n", "radio_program = song_generator(songs)\n", "next(radio_program)\n", "\n", "from random import choice\n", "def song_generator(song_list):\n", " new_song = None\n", " while True:\n", " if new_song != None:\n", " if new_song[0] == \"-songlist-\":\n", " song_list = new_song[1]\n", " new_song = yield choice(song_list)\n", " else:\n", " title, performer = new_song\n", " new_song = title + \" - \" + performer\n", " if new_song not in song_list:\n", " song_list.append(new_song)\n", " new_song = yield new_song\n", " else:\n", " new_song = yield choice(song_list)\n", "songs1 = [\"Après un Rêve - Gabriel Fauré\"\n", " \"On Stream - Nils Petter Molvær\",\n", " \"Der Wanderer Michael - Michael Wollny\",\n", " \"Les barricades mystérieuses - Barbara Thompson\",\n", " \"Monday - Ludovico Einaudi\"]\n", "\n", "songs2 = [\"Dünyadan Uzak - Pinhani\",\n", " \"Again - Archive\",\n", " \"If I had a Hear - Fever Ray\"\n", " \"Every you, every me - Placebo\",\n", " \"Familiar - Angnes Obel\"]\n", "radio_prog = song_generator(songs1)\n", "for i in range(5):\n", " print(next(radio_prog))\n", "\n", "c = count()\n", "for i in range(6):\n", " print(next(c))\n", "print(\"Let us see what the state of the iterator is:\")\n", "\n", "print(\"now, we can continue:\")\n", "for i in range(3):\n", " print(next(c))\n", "\n", "class StateOfGenerator(Exception):\n", " def __init__(self, message=None):\n", " self.message = message\n", "\n", "def count(firstval=0, step=1):\n", " counter = firstval\n", " while True:\n", " try:\n", " new_counter_val = yield counter\n", " if new_counter_val is None:\n", " counter += step\n", " else:\n", " counter = new_counter_val\n", " except StateOfGenerator:\n", " yield (firstval, step, counter)\n", "\n", "\n", "c = count()\n", "for i in range(3):\n", " print(next(c))\n", "print(\"Let us see what the state of the iterator is:\")\n", "i = c.throw(StateOfGenerator)\n", "print(i)\n", "print(\"now, we can continue:\")\n", "for i in range(3):\n", " print(next(c))\n", "\n", "\n", "def gen1():\n", " for char in \"Python\":\n", " yield char\n", " for i in range(5):\n", " yield i\n", "\n", "def gen2():\n", " yield from \"Python\"\n", " yield from range(5)\n", "\n", "g1 = gen1()\n", "g2 = gen2()\n", "print(\"g1: \", end=\", \")\n", "for x in g1:\n", " print(x, end=\", \")\n", "print(\"\\ng2: \", end=\", \")\n", "for x in g2:\n", " print(x, end=\", \")\n", "print()\n", "\n", "def cities():\n", " for city in [\"Berlin\", \"Hamburg\", \"Munich\", \"Freiburg\"]:\n", " yield city\n", "\n", "def squares():\n", " for number in range(10):\n", " yield number ** 2\n", "\n", "def generator_all_in_one():\n", " for city in cities():\n", " yield city\n", " for number in squares():\n", " yield number\n", "\n", "def generator_splitted():\n", " yield from cities()\n", " yield from squares()\n", "\n", "lst1 = [el for el in generator_all_in_one()]\n", "lst2 = [el for el in generator_splitted()]\n", "print(lst1 == lst2)\n", "\n", "def subgenerator():\n", " yield 1\n", " return 42\n", "\n", "def delegating_generator():\n", " x = yield from subgenerator()\n", " print(x)\n", "\n", "for x in delegating_generator():\n", " print(x)\n", "\n", "\n", "def permutations(items):\n", " n = len(items)\n", " if n==0: yield []\n", " else:\n", " for i in range(len(items)):\n", " for cc in permutations(items[:i]+items[i+1:]):\n", " yield [items[i]]+cc\n", "\n", "for p in permutations(['r','e','d']): print(''.join(p))\n", "for p in permutations(list(\"game\")): print(''.join(p) + \", \", end=\"\")\n", "\n", "def k_permutations(items, n):\n", " if n==0:\n", " yield []\n", " else:\n", " for item in items:\n", " for kp in k_permutations(items, n-1):\n", " if item not in kp:\n", " yield [item] + kp\n", "\n", "for kp in k_permutations(\"abcd\", 3):\n", " print(kp)\n", "'''\n", "def fibonacci():\n", " \"\"\" A Fibonacci number generator \"\"\"\n", " a, b = 0, 1\n", " while True:\n", " yield a\n", " a, b = b, a + b\n", "\n", "print(list(fibonacci(5, 10)))\n", "'''\n", "def running_average():\n", " total = 0.0\n", " counter = 0\n", " average = None\n", " while True:\n", " term = yield average\n", " total += term\n", " counter += 1\n", " average = total / counter\n", "\n", "\n", "ra = running_average() # initialize the coroutine\n", "next(ra) # we have to start the coroutine\n", "for value in [7, 13, 17, 231, 12, 8, 3]:\n", " out_str = \"sent: {val:3d}, new average: {avg:6.2f}\"\n", " print(out_str.format(val=value, avg=ra.send(value)))\n" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "Hsy1-cmcMiPG", "outputId": "071ed251-9609-4dc2-fbd1-8ff76675acd6" }, "execution_count": 16, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Après un Rêve - Gabriel FauréOn Stream - Nils Petter Molvær\n", "Après un Rêve - Gabriel FauréOn Stream - Nils Petter Molvær\n", "Der Wanderer Michael - Michael Wollny\n", "Monday - Ludovico Einaudi\n", "Après un Rêve - Gabriel FauréOn Stream - Nils Petter Molvær\n", "0\n", "1\n", "2\n", "3\n", "4\n", "5\n", "Let us see what the state of the iterator is:\n", "now, we can continue:\n", "6\n", "7\n", "8\n", "0\n", "1\n", "2\n", "Let us see what the state of the iterator is:\n", "(0, 1, 2)\n", "now, we can continue:\n", "2\n", "3\n", "4\n", "g1: , P, y, t, h, o, n, 0, 1, 2, 3, 4, \n", "g2: , P, y, t, h, o, n, 0, 1, 2, 3, 4, \n", "True\n", "1\n", "42\n", "red\n", "rde\n", "erd\n", "edr\n", "dre\n", "der\n", "game, gaem, gmae, gmea, geam, gema, agme, agem, amge, ameg, aegm, aemg, mgae, mgea, mage, maeg, mega, meag, egam, egma, eagm, eamg, emga, emag, ['a', 'b', 'c']\n", "['a', 'b', 'd']\n", "['a', 'c', 'b']\n", "['a', 'c', 'd']\n", "['a', 'd', 'b']\n", "['a', 'd', 'c']\n", "['b', 'a', 'c']\n", "['b', 'a', 'd']\n", "['b', 'c', 'a']\n", "['b', 'c', 'd']\n", "['b', 'd', 'a']\n", "['b', 'd', 'c']\n", "['c', 'a', 'b']\n", "['c', 'a', 'd']\n", "['c', 'b', 'a']\n", "['c', 'b', 'd']\n", "['c', 'd', 'a']\n", "['c', 'd', 'b']\n", "['d', 'a', 'b']\n", "['d', 'a', 'c']\n", "['d', 'b', 'a']\n", "['d', 'b', 'c']\n", "['d', 'c', 'a']\n", "['d', 'c', 'b']\n", "sent: 7, new average: 7.00\n", "sent: 13, new average: 10.00\n", "sent: 17, new average: 12.33\n", "sent: 231, new average: 67.00\n", "sent: 12, new average: 56.00\n", "sent: 8, new average: 48.00\n", "sent: 3, new average: 41.57\n" ] } ] }, { "cell_type": "markdown", "source": [ "# **Syntax of enumerate() method**\n", "enumerate(iterable, start=0)\n", "\n", "Parameters:\n", "Iterable: any object that supports iteration\n", "Start: the index value from which the counter is to be started, by default it is 0\n", "Return:\n", "Returns an iterator containing a tuple of index and element from the original iterable\n", "Using a Custom Start Index\n", "By default enumrate() starts from index 0. We can customize this using the start parameter. if want the index to begin at value other than 0." ], "metadata": { "id": "M7T4hnLhMiXV" } } ] }