Convert Generator to List in Python

Lists and Generators in Python

In Python, a list is iterable where every element gets stored at some specific index in a contiguous memory location. We can access the elements from their indexes.

Generator objects resemble iterators in Python but do not store elements in the memory. Both lists and generators represent a sequence of elements in Python. Let us understand more about generator objects in Python.

The return keyword is used to return some value from a function. The yield keyword in Python works similarly. It sends a value from the function to the caller and then resumes the function execution. Any function that has a yield keyword, is termed a generator function.

Using generators functions in Python, we can send a sequence of elements to the caller. These functions return a generator object which can be iterated over. We can iterate over the generator object using a for loop.

We can also use a generator expression to initiate a generator object. A generator expression is similar to list comprehension where we run a for loop in a single line of code to create a sequence.

The values from a generator can be stored in an actual data object. We can convert generator to list in Python.

Ways to convert generator to list in Python

We will now discuss how to convert generator to list in Python.

Using the list() function to convert generator to list in Python

The list() constructor is used to initiate list objects. We can pass a generator object to this function to convert generator to list in Python.

For example,

Output:

[0, 1, 2, 3, 4] [5, 6]

In the above example,

  • We create two generator objects using a generator function and a generator expression.
  • We convert them to a list using the list() function and print the lists.

Using the unpack operator * to convert generator to list in Python

The unpack operator is used to unpack all the elements from an iterable. Since a generator object acts like an iterable, we can unpack the elements from the generator and store them in a list.

See the code below.

Output:

[0, 1, 2, 3, 4] [5, 6]

In the above example, we unpack the elements of both the generator objects to a list.

Using the list comprehension to convert generator to list in Python

List comprehension is an elegant method to create lists in a single line of code. A generator expression can create generator objects and is very similar to the list comprehension method.

We can put the generator expression within the square brackets ([]) to emulate list comprehension. This method will convert generator to list in Python and the sequence will be stored in the list.

For example,

Output:

[0, 1, 2, 3, 4]

Using the append() function to convert generator to list in Python

A list is a mutable object and we can add elements to a list after defining it. The append() function adds elements to the end of a list.

A generator object can be iterated by using a for loop. We can access the elements of the sequence and append them individually to a list.

See the following example.

Output:

[0, 1, 2, 3, 4] [5, 6]

In the above example,

  • We iterate over the two generator objects using a for loop.
  • We append the elements to an empty list in every iteration using the append() function.

Conclusion

In this article, we discussed how to convert generator to list in Python. We started by highlighting what a generator object is and its comparisons with a list. The most simple method involves the use of the list() constructor. We can also unpack the elements into a list. The list comprehension method uses a generator expression to convert generator to list in Python. We can also iterate over the sequence of the generator object and append the elements individually.

That’s all about how to convert Generator to List in Python,

Was this post helpful?


import_contacts

You may also like:

Related Posts

  • 24 May

    Replace Comma with Space in List in Python

    Table of ContentsHow to replace comma with space in list in PythonUsing the replace() function to replace comma with space in list in PythonUsing the re.sub() function to replace comma with space in list in PythonUsing the split() and join() functions to replace comma with space in list in Python.ConclusionWas this post helpful? In this […]

  • 19 May

    Compare list elements with each other in Python

    Table of ContentsCompare all elements in list PythonWays to compare list elements with each other in PythonUsing the for loop to compare all elements in list PythonUsing the itertools.combinations() function to compare all elements in list PythonConclusionWas this post helpful? In this post, we will see how to compare list elements with each other in […]

  • 18 May

    How to Deep Copy a List in Python

    Table of ContentsWhat is a list in Python?What are shallow copy and deep copy operations in Python?Shallow CopyDeep CopyHow to deep copy a list in Python?Using the deepcopy() function from the copy module to deep copy a list in Python.Using a user-defined function to deep copy a list in Python.Using list comprehension to deep copy […]

  • Create a list from 1 to 100 in Python
    03 May

    Create a List from 1 to 100 in Python

    Table of ContentsWays to create a list from 1 to 100 in PythonUsing the range() function to create a list from 1 to 100 in PythonUsing the numpy.arange() function to create a list from 1 to 100 in PythonUsing the for loop with range() to create a list from 1 to 100 in PythonConclusionWas this […]

  • 07 January

    How to compare lists in Python

    Table of ContentsPython compare listsHow to compare lists in PythonUsing the sort() function and == operatorUsing the map() and reduce() functionsUsing the set() function and == operatorUsing the for loopUsing the set() function and - operatorUsing the collections.Counter() class and == operatorUsing the zip(), sum(), and len() functionsUsing the cmp() functionConclusionWas this post helpful? In […]

  • Python List of dictionaries
    09 December

    List of Dictionaries in Python

    Table of ContentsHow to Create a List of Dictionaries in Python?How to Access Elements of a List of Dictionaries?Search in a List of Dictionaries in PythonUpdate Values in a List of Dictionaries in PythonHow to Sort List of Dictionaries in Python?ConclusionWas this post helpful? We use both lists and dictionaries in our python programs. Lists […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.