I'd like to rewrite this part of code using generator :
basic = []
for x in range(0,11):
basic.append(x**2)
How can I do this ? Tried :
basic.append(x**2 for x in range(0,11))
but it raises syntax error in x**2
part.
I'd like to rewrite this part of code using generator :
basic = []
for x in range(0,11):
basic.append(x**2)
How can I do this ? Tried :
basic.append(x**2 for x in range(0,11))
but it raises syntax error in x**2
part.
You are mistaken; your code doesn't produce a syntax error, it just does the wrong thing:
If you must use a generator:
It's simpler to use a list comprehension:
Use
extend
notappend
.Better yet:
or
You'd be better off using list comprehension: