Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

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 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 :: upper method in python 
Python :: Lists and for loops in python 
Python :: importare un foglio di un file excel in python 
Python :: read://https_www.tumblr.com/?url=https://www.tumblr.com/login?redirect_to=%2Fneue_web%2Fiframe%2Freblog%2F629907744681590784%2FjQw7OUs8&3739a18c-0c68-43cc-a4cb-b8b99e9bfd72=a52e06db-92b6-4b86-b3c5-fa2ab267405c 
Python :: webhook logger python 
Python :: 90/360 
Python :: django rotatingfilehandler 
Python :: python define propery by null 
Python :: split column in exact spot python 
Python :: datetime day deutsch python 
Python :: get resource path python 
Python :: python initialize a 2d array 
Python :: python inline print variable 
Python :: zoom in geopandas polot 
Python :: how to sum a column in csv python using list in python 
Python :: assert_series_equal 
Python :: mutliple inxed conditions py 
Python :: Access the Response Methods and Attributes in python Show redirections 
Python :: python multiple imports 
Python :: flask make_response render_template 
Python :: Pandas index column title or name 
Python :: sklearn standardscaler for numerical columns 
Python :: bee swarm plot 
Python :: python list as stacks 
Python :: ABA Alphabet pyramid 
Python :: dimension reduction using pca 
Python :: hashing algorithms in python 
Python :: website screenshot python compress image 
Python :: read the entire images in the dataset 
Python :: Horizontal concatication 
ADD CONTENT
Topic
Content
Source link
Name
6+5 =