Convert Generator Object To List in Python
Last Updated :
06 Feb, 2024
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.
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))
Output<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))
Output<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))
Output<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))
Output<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.
Similar Reads
Convert Generator Object To JSON In Python
JSON (JavaScript Object Notation) is a widely used data interchange format, and Python provides excellent support for working with JSON data. However, when it comes to converting generator objects to JSON, there are several methods to consider. In this article, we'll explore some commonly used metho
2 min read
Convert nested Python dictionary to object
Let us see how to convert a given nested dictionary into an object Method 1 : Using the json module. We can solve this particular problem by importing the json module and use a custom object hook in the json.loads() method. python3 # importing the module import json # declaringa a class class obj: #
2 min read
Convert class object to JSON in Python
In Python, class objects are used to organize complex information. To save or share this information, we need to convert it into a format like JSON, which is easy to read and write. Since class objects can't be saved directly as JSON, we first convert them into a dictionary (a data structure with ke
3 min read
Python - Convert Dictionary Object into String
In Python, there are situations where we need to convert a dictionary into a string format. For example, given the dictionary {'a' : 1, 'b' : 2} the objective is to convert it into a string like "{'a' : 1, 'b' : 2}". Let's discuss different methods to achieve this:Using strThe simplest way to conver
2 min read
Convert Object to String in Python
Python provides built-in type conversion functions to easily transform one data type into another. This article explores the process of converting objects into strings which is a basic aspect of Python programming.Since every element in Python is an object, we can use the built-in str() and repr() m
2 min read
Python - Convert list of dictionaries to JSON
In this article, we will discuss how to convert a list of dictionaries to JSON in Python. Python Convert List of Dictionaries to JsonBelow are the ways by which we can convert a list of dictionaries to JSON in Python: Using json.dumps()Using json.dump()Using json.JSONEncoderUsing default ParameterDi
5 min read
How to create a list of object in Python class
In Python, creating a list of objects involves storing instances of a class in a list, which allows for easy access, modification and iteration. For example, with a Geeks class having attributes name and roll, you can efficiently manage multiple objects in a list. Let's explore different methods to
3 min read
Python | Convert list of tuple into dictionary
Given a list containing all the element and second list of tuple depicting the relation between indices, the task is to output a dictionary showing the relation of every element from the first list to every other element in the list. These type of problems are often encountered in Coding competition
8 min read
Convert JSON to dictionary in Python
JSON stands for JavaScript Object Notation. It means that a script (executable) file which is made of text in a programming language, is used to store and transfer the data. Python supports JSON through a built-in package called JSON. To use this feature, we import the Python JSON package into Pytho
4 min read
Convert Tuple to List in Python
In Python, tuples and lists are commonly used data structures, but they have different properties:Tuples are immutable: their elements cannot be changed after creation.Lists are mutable: they support adding, removing, or changing elements.Sometimes, you may need to convert a tuple to a list for furt
2 min read