Unable to print sorted output of an array


#1

Non working screnario:

start_list = [5, 3, 1, 2, 4]
square_list = []
for x in start_list:
y = (x ** 2)
square_list.append(y)
print (square_list.sort())

Its returning None value.

Is it not possible to print directly with sorted error.

Working screnario:

start_list = [5, 3, 1, 2, 4]
square_list = []
for x in start_list:
y = (x ** 2)
square_list.append(y)
square_list.sort()
print (square_list)


#2

what is your question?


#3

Hi Stetim,

Hope you are doing good.

My expectation is take few number & square that number Finally sort those number.

Problem is :

tart_list = [5, 3, 1, 2, 4]
square_list = []
for x in start_list:
y = (x ** 2)
square_list.append(y)
print (square_list.sort())

Its returning None value.

If i print in other line its ok.

I just want to know why i’m not able to print directly for sorting of array list.


#4

Because .sort() is modifying the original list, and the .sort() method returns None. Which is why you can’t print directly


#5

Got it, Thanks Stetim94


#6

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