Convert Generator Object to List in Python (3 Examples)
Hi! This tutorial will show you 3 ways to transform a generator object to a list in the Python programming language.
The table of content is structured as follows:
Let’s jump into the Python code!
Create Sample Generator Object
Here, we will create the sample generator object that will be changed into a Python list using tuple comprehension. Therefore, in your preferred Python IDE, run the line of code below to create the sample generator object:
gen_obj = (x for x in range(5)) print(type(gen_obj)) # <class 'generator'>
However, in each of the examples that follow, the generator object will need to be run first in order for it to be turned into a list of integers; otherwise, it will print out an empty list.
Example 1: Change Generator Object to List Using list() Constructor
In this first example, we will turn the generator object to a list using the list() constructor:
gen_obj = (x for x in range(5)) list_from_gen = list(gen_obj) print(list_from_gen) # [0, 1, 2, 3, 4] print(type(list_from_gen)) # <class 'list'>
In the above code, the list()
constructor changes the generator object into a list of integers, starting at 0.
Example 2: Change Generator Object to List Using extend() Method
In this next example, we will transform the generator object to a list using the extend() method:
gen_obj = (x for x in range(5)) list_from_gen = [] list_from_gen.extend(gen_obj) print(list_from_gen) # [0, 1, 2, 3, 4] print(type(list_from_gen)) # <class 'list'>
In the above example, we first created an empty list with the square brackets [], and then we populated that empty list with the elements of the generator object using the extend()
method.
Example 3: Change Generator Object to List Using List Comprehension
In this final example, we will change the generator object to a list using list comprehension like so:
gen_obj = (x for x in range(5)) list_from_gen = [x for x in gen_obj] print(list_from_gen) # [0, 1, 2, 3, 4] print(type(list_from_gen)) # <class 'list'>
The list comprehension in the above example enabled us to iterate through the generator object and store its elements inside in a list.
With that, I hope you have learned how to convert a generator object to a list in Python. I hope you found this tutorial helpful!
Video, Further Resources & Summary
Do you need more explanations on how to convert a generator object to a list 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 convert a generator object to a list 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:
- count() Method for Lists in Python (2 Examples)
- Access Elements in List within Dictionary in Python (2 Examples)
- Convert List of Tuples to List of Lists in Python (3 Examples)
- Learn Python Programming
- Convert List from Boolean to String in Python (2 Examples)
- Convert List to Matrix & Vice-Versa in in Python (Examples)
This post has shown how to convert a generator object to a list in Python. In case you have further questions, you may leave a comment below.
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.
Statistics Globe Newsletter