Convert List to Iterator & Vice Versa in Python (Examples)

 

Hello! This tutorial will show you how to turn a list into an iterator and vice-versa in the Python programming language.

First, though, here is a quick overview of this tutorial:

Let’s get right into the Python code!

 

Create Sample List

Here, we will create a Python list (iterable) that will be turned into an iterator. An iterator is an object that holds a countable number of values and allows you to iterate over a collection of data to be one item at a time.

To create the sample list, in your preferred Python coding IDE, run the line of code below:

my_list = [1, 2, 3, 4, 5]
 
print(type(my_list))
 
# <class 'list'>

 

Example 1: List to Iterator | Turn List to Iterator Using iter() Function

In this first example, we will use Python’s built-in iter() function to turn the list to an iterator.

my_iterator = iter(my_list)
 
 
print(type(my_iterator))
 
# <class 'list_iterator'>

You can now print each value in the list using the next() and print() functions. This wouldn’t be possible if my_list was not converted into an iterator.

print(next(my_iterator))
print(next(my_iterator))
print(next(my_iterator))
print(next(my_iterator))
print(next(my_iterator))
 
#1
#2
#3
#4
#5

As shown, the items could be printed one by one thanks to the data type change.

 

Example 2: List to Iterator | Turn List to Iterator Using List Comprehension

In this second example, we will use list comprehension to turn the list into an iterator.

my_iterator = (x for x in my_list)
 
print(next(my_iterator))
print(next(my_iterator))
print(next(my_iterator))
print(next(my_iterator))
print(next(my_iterator))
 
#1
#2
#3
#4
#5
 
 
print(type(my_iterator))
 
# <class 'list_iterator'>

The list comprehension creates a generator expression that produces an iterator over the elements in “my_list”. The used parentheses, instead of square brackets, indicate that the result is not a list but rather an iterator.

We have now demonstrated how to convert a list to an iterator in Python. Next, we will demonstrate how to turn the iterator into a list back.

 

Example 1: Iterator to List | Turn Iterator to List Using list() Function

In this example, we will use the built-in list() function to turn the iterator object back into a list.

new_list = list(my_iterator)
print(new_list)
 
# [1, 2, 3, 4, 5]
 
 
print(type(new_list))
 
# <class 'list'>

As seen, new_list is indeed a list and identical to the initial my_list.

 

Example 2: Iterator to List | Turn Iterator to List Using a For Loop

In this second example, we will use a for loop to convert the iterator object into a list.

new_list = []
for item in my_iterator:
    new_list.append(item)
print(new_list)
 
# [1, 2, 3, 4, 5]
 
 
print(type(new_list))
 
# <class 'list'>

With the for loop, we iterated over the elements in the iterator object and appended them to the list new_list.

 

Example 3: Iterator to List | Turn Iterator to List Using List Comprehension

In this third example, we will use list comprehension to convert the iterator object into a list:

new_list = [item for item in my_iterator]
print(new_list)
 
# [1, 2, 3, 4, 5]
 
 
print(type(new_list))
 
# <class 'list'>

The list comprehension method works exactly like the for loop; only that list comprehension is more concise.

With that, we have demonstrated how to turn a list into an iterator and vice-versa in Python. I hope you found this tutorial helpful!
 

Video, Further Resources & Summary

Do you need more explanations on how to turn a list into an iterator and vice-versa in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

In the video, we explain in some more detail how to turn a list into an iterator and vice-versa in Python.

 

The YouTube video will be added soon.

 

Furthermore, I encourage you to check out other interesting Python list tutorials on Statistics Globe, starting with these ones:

This post has shown how to turn a list into an iterator and vice-versa in Python. In case you have further questions, you may leave a comment below.

 

R & Python Expert Ifeanyi Idiaye

This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.

Top