Python iter() method (original) (raw)
Last Updated : 11 Dec, 2024
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))
`
Table of Content
Syntax of
iter()
methoditerator = iter(iterable)
**Parameters
iterable
: Any object capable of returning its elements one at a time. Examples include lists, tuples, dictionaries, and strings.**Return Type
- Returns an **iterator object that can be used with the
next()
function or afor
loop to access the elements sequentially.
**Examples of iter()
Method
**Using iter()
with String
Python `
Convert string to iterator
s = "Python" iterator = iter(s)
print(next(iterator))
print(next(iterator))
`
**Using iter()
with Dictionary
Python `
d = {'a': 1, 'b': 2, 'c': 3} iterator = iter(d)
for key in iterator: print(key)
`
**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)
`
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 a 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, 4 min read
- Python hasattr() method Python hasattr() function is an inbuilt utility function, which is used to check if an object has the given named attribute and return true if present, else false. In this article, we will see how to check if an object has an attribute in Python. Syntax of hasattr() function Syntax : hasattr(obj, ke 2 min read
- isinstance() method - Python isinstance() is a built-in Python function that checks whether an object or variable is an instance of a specified type or class (or a tuple of classes), returning True if it matches and False otherwise, making it useful for type-checking and ensuring safe operations in dynamic code. Example: [GFGTA 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 f 5 min read
- Python List sort() Method The sort() method in Python is a built-in function that allows us to sort the elements of a list in ascending or descending order and it modifies the list in place which means there is no new list created. This method is useful when working with lists where we need to arranged the elements in a spec 4 min read
- Python List count() method The count() method is used to find the number of times a specific element occurs in a list. It is very useful in scenarios where we need to perform frequency analysis on the data. Let's look at a basic example of the count() method. [GFGTABS] Python a = [1, 2, 3, 1, 2, 1, 4] c = a.count(1) print(c) 2 min read
- Python - __lt__ magic method Python __lt__ magic method is one magic method that is used to define or implement the functionality of the less than operator "<" , it returns a boolean value according to the condition i.e. it returns true if a<b where a and b are the objects of the class. Python __lt__ magic method Syntax S 2 min read
- Python | Decimal exp() method Decimal#exp() : exp() is a Decimal class method which returns the value of the (natural) exponential function e**x at the given number Syntax: Decimal.exp() Parameter: Decimal values Return: the value of the (natural) exponential function e**x at the given number Code #1 : Example for exp() method # 2 min read