Sorting a list with numbers in it:
Question:
I’m trying to sort a list:
x=['1', '3']
using x.sort()
I want it to sort in ascending order but when I print out the result I get “none”
x=['1', '3']
r=x.sort()
print(r)
Answers:
sort
method sorts the elements in-place (it modifies the existing list) and returns None
.
So, just print the x
after using sort
.
x = ['1', '3']
x.sort()
print(x)
If you want to assign the result to a new variable use sorted
.
I’m trying to sort a list:
x=['1', '3']
using x.sort()
I want it to sort in ascending order but when I print out the result I get “none”
x=['1', '3']
r=x.sort()
print(r)
sort
method sorts the elements in-place (it modifies the existing list) and returns None
.
So, just print the x
after using sort
.
x = ['1', '3']
x.sort()
print(x)
If you want to assign the result to a new variable use sorted
.