Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

generator expressions python

>>> even_squares = (x * x for x in range(10)
                    if x % 2 == 0)
Comment

Python Generator Expression

# Initialize the list
my_list = [1, 3, 6, 10]

# square each term using list comprehension
list_ = [x**2 for x in my_list]

# same thing can be done using a generator expression
# generator expressions are surrounded by parenthesis ()
generator = (x**2 for x in my_list)

print(list_)
print(generator)
Comment

generator expressions python

>>> listcomp = ['Hello' for i in range(3)]
>>> genexpr = ('Hello' for i in range(3))
Comment

generator expressions python

>>> genexpr = ('Hello' for i in range(3))
>>> list(genexpr)
['Hello', 'Hello', 'Hello']
Comment

generator expressions python

>>> iterator = ('Hello' for i in range(3))
>>> for x in iterator:
...     print(x)
'Hello'
'Hello'
'Hello'
Comment

generator expressions python

def generator():
    for item in collection:
        yield expression
Comment

generator expressions python

>>> for x in even_squares:
...     print(x)
0
4
16
36
64
Comment

generator expression python

#Generator expressions

#List comprehension is greedy evaluation, create lists immediately when execute it
#Generator expressions is lazy evaluation, creates an iterable generator object on demand
#Generator uses () instead of []
#Syntax order changes slightly compared to list comprehension

numbers = [1, 2, 3, 4, 5, 6, 7, 8]
for val in (x**2 for x in numbers if x%2 != 0):     # note the () instead of []
    print(val, end = " ")
#1 9 25 49 
Comment

PREVIOUS NEXT
Code Example
Python :: python try except: print error 
Python :: python telegram bot login 
Python :: python built in functions 
Python :: extract coordinate values in xarray 
Python :: beautifulsoup getting data from a website 
Python :: python parse int as string 
Python :: how to make a comment in python 
Python :: update all pip packages 
Python :: how to capture video in google colab with python 
Python :: matrix multiplication nupy 
Python :: Box Plot, Python 
Python :: read csv python 
Python :: f readlines python not working 
Python :: The datetime and django.utils.timezone modules are available, so you can do e.g. timezone.now 
Python :: xlsb file in pandas 
Python :: Javascript rendering html 
Python :: basic python programs 
Python :: django change settings at runtime 
Python :: add icon to exe file 
Python :: text classification 
Python :: append a dataframe to an empty dataframe 
Python :: logistic regression algorithm 
Python :: download latest chromedriver python code 
Python :: python oauthlib 
Python :: python compare each item of one list 
Python :: python extract all characters from string before a character 
Python :: select rows with multiple conditions pandas query 
Python :: inpuit inf terfminal ppython 
Python :: calculate the shortest path of a graph in python 
Python :: phyton 2.7 convert timedelta to string 
ADD CONTENT
Topic
Content
Source link
Name
1+6 =