I've been sorting list's values in ascending order and I found to cast the list into set and thereafter cast it back into list solves the problem for example
attempted = [2, 4, 1, 3, 5]
print(list(set(attempted))) # output [1, 2, 3, 4, 5]
and also is the same when list
is made up by alphabets but when numbers are in the form of string(str
) this does nothing, for example
lit = ['2','4', '1', '3', '5']
print(list(set(lit))) # output keeps changing by the time
but when sorted
method is applied to the list it respects the ascending order like
lit = ['2','4', '1', '3', '5']
res = sorted(list(set(lit)))
print(res) # ['1', '2', '3', '4', '5']
and I don't understand why set can't sort the numbers which are in the form of string(str
) by default without adding sorted
method.
waiting for your good answers, suggestion and documentations about it