Convert Generator To List In Python
Looking for a tutorial on how to convert generator to list in python then you are at the right place, today in this tutorial I will show you how to convert generator to list in python so follow this tutorial till the end.
A generator is like a normal function but instead of using the return keyword a generator function uses a yield keyword, when a function uses yield keyword it is called as a generator below is an example.
# Normal Function def normal_func(): return 1 # Generator def gen_func(): yield 1
When we print the generator function it returns a generator object, so today we want to convert a generator function into a list. The list will contain all the yield values of the generator.
Convert Generator To List In Python Code
def nums_gen(): yield 1 yield 2 yield 3 yield 4 yield 5 # prints a generator object print(nums_gen()) # converting generator to list using list() nums_gen = list(nums_gen()) # prints a list print(nums_gen)
Above is the python program to convert a generator to list. You can run this code and see the result. To run this program you need to have python installed, if you don’t have, then read this: Install and setup python.
Follow this steps to run this program:
- First, create a new folder for this program.
- Open it in a code editor of your choice.
- Create a python file with .py extension.
- Copy and paste the above code in the file.
- Open a command prompt at the program folder.
- To run, use this command python filename.py.
Follow all the above steps if you don’t know how to run a python program. You can also run this program by using this online python compiler. After running the program you will see the generator converted to list like below
[1, 2, 3, 4, 5]
As you can see, we have successfully converted a generator object to list in python. Now let’s see how this program works.
Convert Generator To List In Python Explanation
We have the code and how to run this program now. Let’s see how this program works and converts a generator to a list in python.
def nums_gen(): yield 1 yield 2 yield 3 yield 4 yield 5 # prints a generator object print(nums_gen())
First we have created a generator function, We know that it is a generator function because it uses yield instead of return.
When we print this generator function we get a generator object from that we also know that it is a generator.
# converting generator to list using list() nums_gen = list(nums_gen()) # prints a list print(nums_gen)
Here we used the list() method which converts a sequence type into a list, for example a tuple will convert to list.
So we used the list() method and gave it a generator and stored the result in the same variable, and we print and see a list with all the yield values.
This was the tutorial on converting a generator to list in python. I hope you found this tutorial helpful and useful. Do share this tutorial with your friends who might need this or find it helpful.
Here are some more python programs you will find helpful:
- Convert GPX to CSV file in python.
- Convert IPYNB file to python file.
- Convert Wav to mp3 format in python.
- Convert Miles to feet in python.
I hope you found what you were looking for from this python tutorial and if you want more python tutorials like this, do join our Telegram channel to get updated.
Thanks for reading, have a nice day 🙂