guys i m not able to figure out why the output comes none(i expect sorted list to be printed) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

guys i m not able to figure out why the output comes none(i expect sorted list to be printed)

https://code.sololearn.com/cHEvmvnlI6QB/?ref=app

10th Jul 2019, 6:38 PM
PRO
PRO - avatar
5 Answers
+ 1
The list.sort() method sorts the list in-place (i.e. doesn't return any value). cars = ["hyundai", "tesla", "lambo", "bugatti"] cars.sort() print(cars) There is also a sorted() function that returns a sorted list. sorted_cars = sorted(cars) print(sorted_cars) https://docs.python.org/3.7/howto/sorting.html
10th Jul 2019, 7:28 PM
Diego
Diego - avatar
+ 2
You don't do sorted_cars = cars.sort(). You do cars.sort() without assigning it to a variable. Also, there is no comma between lambo and tesla.
10th Jul 2019, 6:47 PM
CeePlusPlus
CeePlusPlus - avatar
+ 1
ohhh thank u Diego
11th Jul 2019, 1:28 AM
PRO
PRO - avatar
0
CeePlusPlus but why cant i assign a variable to it if i dont wanna change the original list
10th Jul 2019, 6:51 PM
PRO
PRO - avatar
0
CeePlusPlus even print(cars.sort()) isnt working. why???
10th Jul 2019, 6:52 PM
PRO
PRO - avatar