More with 'for'


#1

I passed this exercise but I have no idea why. I mean I was kinda messing around with it and apparently I got it right without no idea what i just did.

this is the code I wrote for this exercise:

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

I really don't understand why appending number ** 2 to the square_list make me pass this exercise.
Could anyone please explain it to me D:

Thanks a lot ! :slight_smile:


#2

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

for number in start_list:
    print number ** 2

square_list.append(25)
square_list.append(9)
square_list.append(1)
square_list.append(4)
square_list.append(16)

square_list.sort()
print square_list

This is the way i used and passed, finally. I literally did what the directions said...I couldn't figure out that short cut you used. I didn't even think using the square_list command in the for loop would work. Very insightful.


#3

I think the purpose to .append the list items into square_list is to keep it a list, compared to assigning the values to square _list the usual way like defining functions.

exp:

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

 # Your code here!
 for numbers in start_list:
     square_list = numbers ** 2
     print square_list
 square_list.sort()

 Traceback (most recent call last):
   File "E:/Documents/Python/Assignment Test.py", line 8, in <module>
     square_list.sort()
 AttributeError: 'int' object has no attribute 'sort'

You receive traceback error indicating " 'int' object has no attribute 'sort' ". Guess that's the reason to .append square_list. I was confused too til' I assigned the values to see if I get the same results, instead I got an error since we're dealing with Lists.


#4

The idea is quite simple you start with a list of numbers and for every number in this list:
You square this number and add it to another list. Once you've done this with every number in the initial list you sort the new list and print it.

Now this for every number in this list part is done by the for loop:

for number in start_list:

Now what .append(value) does (on lists) is to just add the value in its parenthesis to the very end of the list for example:

simple_list = [1]
simple_list.append(2)
print simple_list # -> [1,2]

So what you (@kimkp) did was pretty much what you should do. The only part that is not that necessary is to have the sorting of that list inside of the loop. What it does is that it sorts the list (from lowest to highest number) in every loop but as you aren't doing anything with the list in the loop except adding new numbers you could as well do this sorting once after the loop is done (as @consuellar did).

@consuellar of course you could as well add all the numbers by hand using the append method 5 times in a row but using a for loop requires 2 lines of code for any given number of elements in the list whereas you're version requires 1 line of code for every element in the list. Try this with a list of 100 entries and you might be faster calculating the results by hand :slight_smile:

And as @nimblenavigator realized when you do not append but assign the value you overwrite the list with a number, so you essentially lose all the information but the very last entry of the original list (squared) and you can no longer apply list methods to your variable because now it is no longer a list but just that last number ( in this case an integer number).


#5

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

for number in start_list:

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

Your code here!

print square_list

that the code


#6

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

square_list = []

# Your code here!

def square():
    for n in start_list:
       square_list.append(n ** 2)
       square_list.sort
    
print square_list

Can you please help? I keep getting the error- square list should be the same length as start list


#7

it was formatted when I posted but obviously it changed the format


#8

I got it, thanks anyway


#11

Did it work properly?


#12

That's like manually pasting each number in the list. You need to do it with the help of a loop in order to understand the concept.


#13

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.