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)
Asked By: alibiineed

||

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.

Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.