Python | Generator Expressions Last Updated : 05 Feb, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In Python, to create iterators, we can use both regular functions and generators. Generators are written just like a normal function but we use yield() instead of return() for returning a result. It is more powerful as a tool to implement iterators. It is easy and more convenient to implement because it offers the evaluation of elements on demand. Unlike regular functions which on encountering a return statement terminates entirely, generators use a yield statement in which the state of the function is saved from the last call and can be picked up or resumed the next time we call a generator function. Another great advantage of the generator over a list is that it takes much less memory. In addition to that, two more functions _next_() and _iter_() make the generator function more compact and reliable. Example : Python3 # Python code to illustrate generator, yield() and next(). def generator(): t = 1 print ('First result is ',t) yield t t += 1 print ('Second result is ',t) yield t t += 1 print('Third result is ',t) yield t call = generator() next(call) next(call) next(call) Output : First result is 1Second result is 2Third result is 3Difference between Generator function and Normal function - Once the function yields, the function is paused and the control is transferred to the caller.When the function terminates, StopIteration is raised automatically on further calls.Local variables and their states are remembered between successive calls.The generator function contains one or more yield statements instead of a return statement.As the methods like _next_() and _iter_() are implemented automatically, we can iterate through the items using next().There are various other expressions that can be simply coded similar to list comprehensions but instead of brackets we use parenthesis. These expressions are designed for situations where the generator is used right away by an enclosing function. Generator expression allows creating a generator without a yield keyword. However, it doesn’t share the whole power of the generator created with a yield function. Example : Python3 # Python code to illustrate generator expression generator = (num ** 2 for num in range(10)) for num in generator: print(num) Output : 0149162536496481We can also generate a list using generator expressions : Python3 string = 'geek' li = list(string[i] for i in range(len(string)-1, -1, -1)) print(li) Output: ['k', 'e', 'e', 'g'] Comment More infoAdvertise with us Next Article Python | Generator Expressions C Chinmoy Lenka Improve Article Tags : Python Practice Tags : python Similar Reads Python List Comprehensions vs Generator Expressions What is List Comprehension? It is an elegant way of defining and creating a list. List Comprehension allows us to create a list using for loop with lesser code. What normally takes 3-4 lines of code, can be compressed into just a single line. Example: Python3 # initializing the list list = [] for i 3 min read Generators in Python Python generator functions are a powerful tool for creating iterators. In this article, we will discuss how the generator function works in Python.Generator Function in PythonA generator function is a special type of function that returns an iterator object. Instead of using return to send back a si 5 min read Assignment Operators in Python The Python Operators are used to perform operations on values and variables. These are the special symbols that carry out arithmetic, logical, and bitwise computations. The value the operator operates on is known as the Operand. Assignment Operators are used to assign values to variables. This opera 7 min read Comprehensions in Python Comprehensions in Python provide a concise and efficient way to create new sequences from existing ones. They enhance code readability and reduce the need for lengthy loops. Python supports four types of comprehensions:List ComprehensionsDictionary ComprehensionsSet ComprehensionsGenerator Comprehen 3 min read Python - Itertools.compress() Pythonâs Itertool is a module that provides various functions that work on iterators to produce complex iterators. This module works as a fast, memory-efficient tool that is used either by themselves or in combination to form iterator algebra. Note: For more information, refer to Python Itertools Co 2 min read Like