 # Python 9/14 Help!

I’m completely clueless on this one. Has anyone else managed to get it right?

Here’s my code:

start_list = [5, 3, 1, 2, 4]
square_list =

for number in start_list:
square_list.append(start_list ** 2)
square_list.append(start_list ** 2)
square_list.append(start_list ** 2)
square_list.append(start_list ** 2)
square_list.append(start_list ** 2)

square_list.sort()
print square_list

I currently get this result:

[1, 1, 1, 1, 1, 4, 4, 4, 4, 4, 9, 9, 9, 9, 9, 16, 16, 16, 16, 16, 25, 25, 25, 25, 25]
None

Oops, try again. square_list should be the same length as start_list

4 Likes

you should maybe append (number ** 2)

1 Like

This is my code and it worked:

start_list = [5, 3, 1, 2, 4]
square_list =

for number in start_list:
square_list.append(number ** 2)
square_list.sort()

print square_list

14 Likes

This is my code, I use list comprehension to populate my list. Best of luck!

``````start_list = [5, 3, 1, 2, 4]
square_list = [x**2 for x in start_list]
print(square_list)``````
1 Like

This code works super fine!
Thanks!

Perfect! Thanks, guys!

You can also use lambda’s to solve the problem

``````#if you are using python 3.x
#import functools
start_list = [5, 3, 1, 2, 4]
square_list = map(lambda x  :  x**2, sorted(start_list))
print (square_list)
``````

what the map keyword does it takes a pattern and matches that pattern to every item in the list and return final list with the elements modified.

2 Likes

@rydan

Actually what you are using is map with lambda as an accessory. They are thinking about depreciating map and filter. Another thing of note is that lambda functions carry the same cost to call that a regular function does, but because map converts the code into c it generally will run tons faster than comparable scripts. So if and when they do depreciate map/filter lambda will be unable to forgo most of it’s function cost.

PEP 279:Look for gvr’s comment

2 Likes

I tried this and it gave me a weird mrssage. please explain it to me:

square_list.append(x**2 for x in start_list)

@blogninja16183_54ebc

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)

CORRECTED CODE:

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)]
print(list_to_fill)
``````

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]
def square_this(list_only):
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.

``````def square_this(list_only):
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.

2 Likes

Here is my code:

start_list = [5, 3, 1, 2, 4]
square_list =

# Access each item form start_list and update it in square_list

for x in start_list:
square_list.append(x ** 2)
square_list.sort()
print square_list

Enjoy. Hope it helps.

Output: [1, 4, 9, 16, 25]

1 Like

Thank you ! that’s pretty lucid and easy to understand !

What does append even mean?

Hi megapro97967,

This was my code and it worked: start_list = [5, 3, 1, 2, 4]
square_list =

for number in start_list:
square_list.append(number ** 2)
square_list.sort()

print square_list

Just don’t forget to indent lines 5 and 6! Hey sujak,
append means to add (something) to the end of a written document I am confused as to why the square_list is appended instead of the start_list since the start list holds the list

@uruichi
Is obvious to be confused because first of all you have to understand how memory works in python. You have to understand difference between global variable, function variable and it’s scope.
Variable starts_list already have its value such a list in global area, once declared you cannot change this value in global area, so you have to create a empty variable for use inside of function in its scope.
So please study first of all memory mechanism and how to pass variable to a function.