Convert Generator Object To List in Python
Python, known for its simplicity and versatility, provides developers with a plethora of tools to enhance their coding experience. One such feature is the generator object, which allows for efficient iteration over large datasets without loading them entirely into memory. In this article, we’ll explore the methods behind converting generator objects to lists and how to perform this transformation in Python.
Convert Generator Object To List
Below, are the ways to Convert Generator Object To List in Python.
- Using Built-in Function
- Using List Comprehension
- Using extend() Method
- Using ‘*’ operator
Convert Generator Object To List Using Built-in Function
In this example, the below code defines a Fibonacci number generator using a Python generator function. It creates a generator object `fib_gen` that yields the first 10 Fibonacci numbers. The generator object is then converted to a list `fib_list`.
Python3
# Fibonacci generator def fib(num): a, b = 0 , 1 for _ in range (num): yield a a, b = b, a + b # Create a generator object fib_gen = fib( 10 ) print ( type (fib_gen)) # Convert to list fib_list = list (fib_gen) print (fib_list) print ( type (fib_list)) |
<class 'generator'> [0, 1, 1, 2, 3, 5, 8, 13, 21, 34] <class 'list'>
Convert Generator Object To List Using List Comprehension
In this example, below code creates a generator object `gen` using a generator expression that produces numbers from 0 to 9. It then converts the generator object to a list `lst` using a list comprehension .
Python3
# create a generator object gen = (x for x in range ( 10 )) print ( type (gen)) # convert to list through comprehension lst = [i for i in gen] print (lst) print ( type (lst)) |
<class 'generator'> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] <class 'list'>
Convert Generator Object To List Using extend() Method
In this example, below code creates a generator object `gen` using a generator expression that produces numbers from 0 to 9. It then creates an empty list `lst` and extends it with the values generated by the generator.
Python3
# create a generator object gen = (x for x in range ( 10 )) print ( type (gen)) # create an empty list and extend lst = [] lst.extend(gen) print (lst) print ( type (lst)) |
<class 'generator'> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] <class 'list'>
Convert Generator Object To List Using ‘*’ operator
In this example, ebcode creates a generator object `gen` using a generator expression that produces numbers from 0 to 9. It then unpacks the generator into a list `lst` using the `*` (unpacking) operator, resulting in a list containing the generated values.
Python3
# create a generator object gen = (x for x in range ( 10 )) print ( type (gen)) # unpack generator into a list lst = [ * gen] print (lst) print ( type (lst)) |
<class 'generator'> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] <class 'list'>
Conclusion
In Conclusion, converting a generator object to a list in Python provides a balance between memory efficiency and the need for immediate access to data. This transformation is beneficial for scenarios requiring eager evaluation, caching, or multiple iterations, allowing developers to optimize the performance of their code based on specific requirements.
Contact Us