Python iter() method Last Updated : 11 Dec, 2024 Summarize Comments Improve Suggest changes Share 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 List methods 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 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 Python Dictionary Methods Python dictionary methods is collection of Python functions that operates on Dictionary.Python Dictionary is like a map that is used to store data in the form of a key: value pair. Python provides various built-in functions to deal with dictionaries. In this article, we will see a list of all the fu 5 min read Python __add__() magic method Python __add__() function is one of the magic methods in Python that returns a new object(third) i.e. the addition of the other two objects. It implements the addition operator "+" in Python. Python __add__() Syntax Syntax: obj1.__add__(self, obj2) obj1: First object to add in the second object.obj2 1 min read Python __len__() magic method Python __len__ is one of the various magic methods in Python programming language, it is basically used to implement the len() function in Python because whenever we call the len() function then internally __len__ magic method is called. It finally returns an integer value that is greater than or eq 2 min read Like