Print Generator Object in Python

Generator object in Python

In Python, we generally use functions to return some value. There is another keyword called yield that works in a similar way. It sends a value from the function to the caller and then resumes the function execution.

We use this keyword to create generator functions that can return a sequence of values whenever it is called. These functions return a generator object which can be iterated over. We can iterate over the sequence, but the whole sequence is not stored in the memory.

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

We cannot directly print generator objects in Python. This will be made clear in the code below.

Output:

<generator object at 0x000001E869BC31C8>

In the above example, we can see that it is not possible to display the sequence generated by a generator object.

Ways to Print Generator object in Python

The code in the previous section informs that it is not possible to directly print generator object in Python. The methods below will demonstrate how to print generator object in Python.

Using the for loop to print generator object in Python

As discussed in the previous section, a generator object can be iterated by using a for loop. This way, we can display the elements from the sequence of a generator object one by one.

See the code below.

Output:

0
1
2
3
4
5
6

In the above example,

  • We create two generator objects, one using a function and the other using an expression.
  • We use the for loop to iterate over the two objects and print the sequence one by one.

Using the join() function to print generator object in Python

The join() function can combine the elements of an iterable to a single string. We know that we can treat a generator object as an iterable so we can join the elements of the generator in a single string with the join() function.

First, we need to ensure that all elements of the sequence are a string. So we will use the map() function and apply the str() function to every element of the generator object.

Then we will use the join() function to combine all the elements.

See the code below.

Output:

0 1 2 3 4
5 6

In the above example,

  • The str() function is applied to every element of the generator.
  • This way, every element is converted to a string.
  • We combine the elements and display them.

Using the unpack operator (*) to print generator object in Python

The unpack operator (*) is used to unpack all the elements from an iterable. It can unpack the sequence from a generator and we can display it using the print() function.

See the code below.

Output:

0 1 2 3 4
5 6

In the above example, we unpack all the elements from the generator object and display them.

Using the next() function to print generator object in Python

In Python 2, we use the next() function to manually get the next element in an iterable. This function works with generator objects as well.

To print generator object in Python, we have to manually call the next() function to print the whole sequence.

For example,

Output:

0
1
2
3
4

In the above example, we use the next() function to print every element of the sequence from a generator object in Python.

If we use the next() function after encountering the end of the iterable, then a StopIteration error is returned.

In Python 3, we can also use the __next__() magic function as an alternative in a similar way.

Using the list() function to print generator object in Python

As discussed earlier, a generator object does not store the values of a sequence in memory. We can store the elements of the generator in another object like a list.

We use the list() function to define a list in Python. We can pass the generator object within this function and it will create a list with the elements of the sequence produced by the generator object.

See the code below.

Output:

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

In the above example, we define the two generator objects. We store them in a list using the list() function and print their contents.

Conclusion

In this article, we discussed generator objects and how to print generator objects in Python. We discussed how they work as an iterable in Python. We can directly access the elements using the for loop. We can combine the elements and display them using the join() function. The unpack operator also works with generator objects. The next() function helps to print each element individually. Finally, we can also store the elements of the generator object in other objects like a list and display them.

That’s all about how to print Generator Object in Python.

Was this post helpful?


import_contacts

You may also like:

Related Posts

  • 29 June

    Get Day Name from Date in Python

    Table of ContentsUse the Calendar Module to Get the Name of the Day in PythonUse the strftime() Method to Get the Name of the Day in PythonUse Pandas Timestamp Method to Get the Name of the Day in PythonUse the weekday() Method to Get the Name of the Day in PythonUse the isoweekday() Method to […]

  • 27 June

    Fill Array with Random Numbers in Python

    Table of ContentsFill array with random numbers in PythonWays to fill array with random numbers in PythonUsing the numpy.random.randint() function to fill array with random numbers in PythonUsing the numpy.random.Generator.integers() function to fill array with random numbers in PythonUsing the random.randint() function to fill array with random numbers in PythonConclusionWas this post helpful? Fill array […]

  • 25 June

    Convert Roman Number to Integer in Python

    Table of ContentsHow to Convert Roman Number to Integer in PythonHow to convert roman number to integer in python?Using the if...else statement to convert roman number to integer in Python.Using classes and a dictionary to convert roman number to integer in Python.Using the roman module to convert roman number to integer in Python.ConclusionWas this post […]

  • 11 June

    Remove Single Quotes from String in Python

    Table of ContentsRemove single quotes from string in Python.How to remove single quotes from string in Python?Using the replace() function to remove single quotes from string in Python.Using a for loop to remove single quotes from string in Python.Using the join() function to remove single quotes from string in Python.Using the re module functions to […]

  • 11 June

    Escape Backslash Character in Python

    Table of ContentsEscape backslash character in python.How to use escape backslash character in python?Using the escape backslash character in python to represent whitespace characters.Using the escape backslash character in python to turn special characters into ordinary characters.Using the escape backslash character in f-strings in Python.Using the escape backslash character in raw strings in Python.Was this […]

  • 11 June

    Break out of function in Python

    Table of ContentsBreak out of function in PythonHow to break out of function in Python?Using the return statement to break out of function in PythonUsing the try and except block to break out of function in PythonUsing the sys.exit() function to break out of function in PythonConclusionWas this post helpful? In this post, we will […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.