Hey I am new to Python and I have this exercise I want to complete but I get the following error: 'int' object has no attribute 'sort'. I must use for-loop to take from a list of numbers and make them all square, then print them all out one by one as sorted. Did I use the sort command incorrectly? Or it doesen't even work with numbers? And do I have to use the .append() command to make them all print out one by one?So this is my wannabe code so far:
start_list = [5, 3, 1, 2, 4]square_list = []for square_list in start_list:square_list ** 2print square_list.sort()
Best Answer
There are three issues:
- You're assigning from the
start_list
over yoursquare_list
in the for loop. - You're not appending your result into the list (this would fail anyway, because of the above).
The end result is that your square_list
is not a list and so cannot be sorted.
Compare the following code:
start_list = [5, 3, 1, 2, 4]square_list = []for item in start_list:square_list.append(item ** 2)
Here we iterate over the list taking an item
for each loop. We then square it (using ** 2
) and then append it to square_list
.
- There is also an issue in the print statement:
print square_list.sort()
Here you are using .sort()
on the list and then trying to print
the returned value. But .sort()
sorts the list in place and then returns None
. So this line will always print None
. To print the sorted list you can either use the sorted()
function which returns the sorted list, ready to be passed to print
:
print sorted(square_list)
Or you can sort the list before printing:
square_list.sort()print square_list
The most pythonic solution is
start_list = [5, 3, 1, 2, 4]square_list = [ i ** 2 for i in start_list ]print(sorted(square_list))
or oneliner:
print(sorted(i ** 2 for i in [5, 3, 1, 2, 4]))
Let's dissect your code:
# here you create an empty list and assign it to# square listsquare_list = []# yet here you will assign each item of start_list# to the name square list one by onefor square_list in start_list:# then you square that number, but it is not stored anywheresquare_list ** 2# at this point, square_list contains the last element# of start_list, that is the integer number 4. It does # not, understandably, have the `.sort` method.print square_list.sort()
The straightforward fix would be to do:
start_list = [ 5, 3, 1, 2, 4 ]square_list = []for element in start_list:square_list.append(element ** 2)square_list.sort() # note that printing this would say "None"print square_list
You must understand for loops.
for square_list in start_list:square_list ** 2
square_list
in this example doesn't refer to the empty list you made. It is used as an ambiguous variable to extract values from a list. As such, this is just the each value in the list during each iteration, and they're all integers because that's what's in your list.
Secondly, you're not actually appending the squares to the list, you're just calculating the squares and doing nothing with them. Lastly, the sort method doesn't return anything, it just alters the list without returning a value. Use the equivalent sorted method which doesn't alter the list but does return a new value. I think you'll best understand with some code.
start_list = [5, 3, 1, 2, 4]square_list = []# for loop number is ambiguous variablefor number in start_list: square = number ** 2 # calculate square to add latersquare_list.append(square) # add the calculationprint sorted(square_list) # print sorted version
After loop your square_list
is bound to integer [to be precise, it's value is 4
].
To create list of squared numbers you may use list comprehensions:
square_list = [i**2 for i in start_list]
And then sort it:
square_list.sort()