In order for that to work you have to do a few things first. When you use the list comprehension generator correctly and you append it to a list as you are attempting to do it will stick the generator into the list. Which has its own uses but that’s too advanced for here.
square_list.append(x**2 for x in start_list)
square_list.append([x**2 for x in start_list])
Now when you print out your square_list you should get something like
<generator object <genexpr> at 0x038C93C8> on the output. that is because as I have stated you are appending a generator/list comprehension into it rather than it’s returns.
List Comprehensions are build with several things.
<List to fill> <Operator> < Start Bracket> <First item: Akin to append this> <For loop> <End Bracket>
So with this information we can do things like this.
list_to_fill = [item_to_append for item_to_append in range(50)]
Try those two lines of code and you will see how valuable to can be. Now on to making list comprehension functions.
list_of_numbers_to_square = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
return [squared_number**2 for squared_number in list_only]
Now lets say we only want even squared numbers, ok we can do that too.
return [squared_number**2 for squared_number in list_only if squared_number % 2 == 0]
All in all list comprehension is a great tool that when used correctly can save time and cpu cycles en masse.
Here is the LIST COMPREHENSION DOC and some examples of complex list comprehension uses @line 531.
If you check out the complex example that is code I have written for my Battleship AI as practice, I used list comprehension because it was way faster than the alternative. Right now I doubt you will be able to understand it but it is a good example of complex list comprehension generators.
Note: The lines 531-541 are the AI for hard and that’s where all the complex generators are.
If you still don’t get something I have said ask freely and I will get back to you as soon as I am able.