Error with negative numbers in the list. Wrong exit.

N=int(raw_input())
A=raw_input()
a_list=A.split()
for i in xrange (len(a_list)):
    a_list[i]=int(a_list[i])
s=list(set(a_list))
sorted(s)
print s

      

When I put N = 5 and put all positive values, I get a list s in ascending order, but when I put N = 5 and put negative values, it doesn't give me a list in ascending order. Why?

+3


source to share


1 answer


You are on the right track, but using sorted

, sorted () returns a new sorted list, leaving the original list unaffected

sort()

sorts the list in place, changes the indices of the list, and returns None

We are using cam function sorted by any iterable, list, dict, string, tupple.

Hence, use sorted(),

when you want the sorted object to return and sort()

when you want to modify the list.



Compared to sorted()

, it sort()

works quickly as it is not copied.

Read here: What's the difference between sorted(list)

vs list.sort()

?

DECISION:

    N=int(raw_input())
    A=raw_input()
    a_list=A.split()
    for i in xrange (len(a_list)):
        a_list[i]=int(a_list[i])
    s=list(set(a_list))
    s.sort()
    print(s)

      

+1


source







All Articles