Python iter() method Last Updated : 11 Dec, 2024 Comments Improve Suggest changes Like Article Like Report Python iter() method is a built-in method that allows to create an iterator from an iterable. An iterator is an object that enables us to traverse through a collection of items one element at a time. Let’s start by understanding how iter() works with a simple example. Python a = [10, 20, 30, 40] # Convert the list into an iterator iterator = iter(a) # Access elements using next() print(next(iterator)) print(next(iterator)) Output10 20 Table of ContentSyntax of iter() methodExamples of iter() MethodFrequently Asked Questions on iter() MethodSyntax of iter() methoditerator = iter(iterable)Parametersiterable: Any object capable of returning its elements one at a time. Examples include lists, tuples, dictionaries, and strings.Return TypeReturns an iterator object that can be used with the next() function or a for loop to access the elements sequentially.Examples of iter() MethodUsing iter() with String Python # Convert string to iterator s = "Python" iterator = iter(s) print(next(iterator)) print(next(iterator)) OutputP y Using iter() with Dictionary Python d = {'a': 1, 'b': 2, 'c': 3} iterator = iter(d) for key in iterator: print(key) Outputa b c Using iter() with Callable and Sentinel Python # Generate numbers until sentinel value is encountered import random iterator = iter(lambda: random.randint(1, 5), 3) for num in iterator: print(num) Output5 5 Comment More infoAdvertise with us Next Article Must Do Coding Questions - Topic-wise M manjeet_04 Follow Improve Article Tags : Python python-list Python-Built-in-functions python-list-functions python +1 More Practice Tags : pythonpythonpython-list Similar Reads Interview PreparationInterview Preparation For Software DevelopersMust Coding Questions - Company-wise Must Do Coding Questions - Topic-wiseCompany-wise Practice ProblemsCompany PreparationCompetitive ProgrammingSoftware Design-PatternsCompany-wise Interview ExperienceExperienced - Interview ExperiencesInternship - Interview ExperiencesPractice @GeeksforgeeksProblem of the DayTopic-wise PracticeDifficulty Level - SchoolDifficulty Level - BasicDifficulty Level - EasyDifficulty Level - MediumDifficulty Level - HardLeaderboard !!Explore More...Data StructuresArraysLinked ListStackQueueBinary TreeBinary Search TreeHeapHashingGraphAdvance Data StructuresMatrixStringAll Data StructuresAlgorithmsAnalysis of AlgorithmsSearching AlgorithmsSorting AlgorithmsPattern SearchingGeometric AlgorithmsMathematical AlgorithmsRandomized AlgorithmsGreedy AlgorithmsDynamic ProgrammingDivide & ConquerBacktrackingBranch & BoundAll AlgorithmsProgramming LanguagesCC++JavaPythonC#Go LangSQLPHPScalaPerlKotlinWeb TechnologiesHTMLCSSJavaScriptBootstrapTailwind CSSAngularJSReactJSjQueryNodeJSPHPWeb DesignWeb BrowserFile FormatsComputer Science SubjectsOperating SystemsDBMSComputer NetworkComputer Organization & ArchitectureTOCCompiler DesignDigital Elec. & Logic DesignSoftware EngineeringEngineering MathematicsData Science & MLComplete Data Science CourseData Science TutorialMachine Learning TutorialDeep Learning TutorialNLP TutorialMachine Learning ProjectsData Analysis TutorialTutorial LibraryPython TutorialDjango TutorialPandas TutorialKivy TutorialTkinter TutorialOpenCV TutorialSelenium TutorialGATE CSGATE CS NotesGate CornerPrevious Year GATE PapersLast Minute Notes (LMNs)Important Topic For GATE CSGATE CoursePrevious Year Paper: CS exams Like