0
guys i m not able to figure out why the output comes none(i expect sorted list to be printed)
5 Antworten
+ 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
+ 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.
+ 1
ohhh thank u Diego
0
CeePlusPlus but why cant i assign a variable to it if i dont wanna change the original list
0
CeePlusPlus even print(cars.sort()) isnt working. why???