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 Python | getattr() method 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 Python List methods Python list methods are built-in functions that allow us to perform various operations on lists, such as adding, removing, or modifying elements. In this article, weâll explore all Python list methods with a simple example.List MethodsLet's look at different list methods in Python:append(): Adds an 3 min read Python | getattr() method The getattr() method in Python returns the value of a named attribute of an object. If the attribute is not found then it returns the default value provided. If no default is given and the attribute does not exist then it raises an AttributeError.Python getattr() Method SyntaxSyntax : getattr(obj, k 3 min read Python | getattr() method The getattr() method in Python returns the value of a named attribute of an object. If the attribute is not found then it returns the default value provided. If no default is given and the attribute does not exist then it raises an AttributeError.Python getattr() Method SyntaxSyntax : getattr(obj, k 3 min read Python | getattr() method The getattr() method in Python returns the value of a named attribute of an object. If the attribute is not found then it returns the default value provided. If no default is given and the attribute does not exist then it raises an AttributeError.Python getattr() Method SyntaxSyntax : getattr(obj, k 3 min read Instance method in Python A class is a user-defined blueprint or prototype from which objects are created. Classes provide a means of bundling data and functionality together. Creating a new class creates a new type of object, allowing new instances of that type to be made. Each class instance can have attributes attached to 2 min read Instance method in Python A class is a user-defined blueprint or prototype from which objects are created. Classes provide a means of bundling data and functionality together. Creating a new class creates a new type of object, allowing new instances of that type to be made. Each class instance can have attributes attached to 2 min read Like