 # 9. More with "for" _ what was your mistake here?

I’m writing this to track back what I was thinking while I was solving this part. Some steps are easy to follow, but some are not, while instructions are written simply and easy. I am curious why I can’t understand certain instruction, and trying to share the course of my thoughts.

## I got stuck at 9. More with “for”

``````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
``````

because I thought I cannot mention name of another list in one list.
I tried to figure out how to combine two lists at the same time without like above.
I saw the right answer by others’ Q&A.

Is it just me? If you stuck here, why were you? ( I think reviewing like this helps me to learning it.)

I repeatedly practiced it and it didn’t work.
My code was like below.

``````A_list = [ "1", "4", "5", "6", "7"]
B_list = [ ]

for number in A_list:
B_list.append(number**2)
B_list.sort()

print B_list
``````

Can you see why it didn’t work?

But the next code worked.

``````A_list = [ 1, 4, 5, 6, 7]
B_list = [ ]

for number in A_list:
B_list.append(number**2)
B_list.sort()

print B_list
``````

(It may seem a very easy and simple to more advanced ppl) My mistake here was that I didn’t write the right type of values in the list. To make number**2 possible, it should be in the form of number, but I put “”, and made it as strings.

3 Likes

Hi medouxa,

I notice, on the code that didn’t work, your A_list = [“1” , “2”, “3” etc]

1. The quotation marks, which surround the list indices, thereby turns this list into a ***‘string’****.
2. You cannot then do a numerical calculation (i.e X.append(i**2)) on an alphabetical value.

This is my understanding of your problem in the code here. I hope this helps.

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

like this bro/sis

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

why m i till not able to get my result?
my code i shown above

your “(number**2)” is meant to be "(number ** 2) @aku

No, the problem is that it should be (numbers ** 2) not (number ** 2)

Good luck =)

Using the “for” command was confusing to me. I didn’t (and still do not all the way) understand it’s direct function. I tried doing it this way first:

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

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

square_list.sort()
print square_list

I thought you would have to assign (square_list) to the action of appending (start_list). Now I realize that the “for” command would not be useful in that sense.

I also wondered if you could achieve the same result with this code:

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

square_list = start_list.append(number**2)
square_list.sort()

print square_list

If the above code works, wouldn’t that be easier and more straight forward?

I ended up using this code to get through the lesson:

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

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

square_list.sort()
print square_list

Still confused as to the main purpose of using “for ___ in___:”.

edit 11/15/2015

I have learned that whatever variable you type after “for ___” can be anything you want it to (without using reserved terms that are special in python). You are basically assigning a variable name to the values of the list or dictionary that comes after " in ___". So you could put “for taco in dictionary” and use an indented code afterwards to manipulate the content in the dictionary. Hope this makes sense to someone else too!

2 Likes

square_list.sort() should be outside loop for

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

If square_list placed outside the loop, then it will append only the last value to square list i.e 42 which results in 16 to the square_list 
square_list.append(number
2) and square_list.sort() both should be inside the loop.

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

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