Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

python generators

# Size of generators is a huge advantage compared to list
import sys

n= 80000

# List
a=[n**2 for n in range(n)]

# Generator
# Be aware of the syntax to create generators, lika a list comprehension but with round brakets
b=(n**2 for n in range(n))

print(f"List: {sys.getsizeof(a)} bits
Generator: {sys.getsizeof(b)} bits")
Source by docs.python.org #
 
PREVIOUS NEXT
Tagged: #python #generators
ADD COMMENT
Topic
Name
4+6 =