Python iter() Function – Tutorial with Examples

The iter() function in Python is a built-in function that returns an iterator object. An iterator is an object that can be iterated (looped) upon. The iter() function takes an object, such as a list or a string, and returns an iterator object that can be used to access the elements of the object one by one.

Syntax

iter(object, sentinel)

Parameters

object – an object that needs to be converted to an iterator, such as a list, string, or tuple.

sentinel (optional) – a value that, when encountered in the iterable, terminates the iteration. This parameter is mostly used in the iter() function when working with a file object.

Return Value

The iter() function returns an iterator object that can be used to access the elements of the object one by one.

Examples

Example 1: Iterating through a list

list = [1, 2, 3, 4]
list_iterator = iter(list)
print(next(list_iterator))
print(next(list_iterator))
print(next(list_iterator))
print(next(list_iterator))

Output:

1
2
3
4

In this example, we have defined a list, list, which contains the numbers 1, 2, 3, and 4. The iter() function is then used to get an iterator object from the list, which is stored in the variable list_iterator. We then use the next() function to access the elements of the list one by one, and print each element. The next() function returns the next element in the iterator each time it is called, and raises a StopIteration exception when there are no more elements left in the iterator.

Example 2: Iterating through a string

string = "Hello World"
string_iterator = iter(string)
print(next(string_iterator))
print(next(string_iterator))
print(next(string_iterator))
print(next(string_iterator))
print(next(string_iterator))

Output:

H
e
l
l
o

In this example, we have defined a string, string, which contains the words “Hello World”. The iter() function is then used to get an iterator object from the string, which is stored in the variable string_iterator. We then use the next() function to access the characters of the string one by one, and print each character. The next() function returns the next character in the iterator each time it is called, and raises a StopIteration exception when there are no more characters left in the iterator.

Example 3: Iterating through a tuple

tuple = (1, 2, 3, 4)
tuple_iterator = iter(tuple)
print(next(tuple_iterator))
print(next(tuple_iterator))
print(next(tuple_iterator))
print(next(tuple_iterator))

Output:

1
2
3
4

In this example, we have defined a tuple, tuple, which contains the numbers 1, 2, 3, and 4. The iter() function is then used to get an iterator object from the tuple, which is stored in the variable tuple_iterator. We then use the next() function to access the elements of the tuple one by one, and print each element. The next() function returns the next element in the iterator each time it is called, and raises a StopIteration exception when there are no more elements left in the iterator.

Use Cases

The iter() function is useful when you need to loop over an object multiple times. It can also be useful when you need to access the elements of an object one by one, without creating a list of all the elements in memory. This can be particularly useful when working with large objects that would consume a lot of memory if stored as a list.

Another use case for the iter() function is when working with file objects. The iter() function can be used with a file object to read the contents of a file line by line, without having to store the entire contents of the file in memory. This can be useful when working with large files that would consume a lot of memory if read into memory all at once.

In conclusion, the iter() function is a versatile and useful function in Python, and should be considered when working with any kind of object that needs to be looped over or accessed one element at a time.

Leave a Reply

Your email address will not be published. Required fields are marked *