3
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 the list s in increasing order, but when I put N=5 and put negative values, It does not give me a list in increasing order. Why ?

Ironman10
  • 237
  • 1
  • 3
  • 10

1 Answers1

1

You are on the right track, but you are using sorted, sorted() returns a new sorted list, leaving the original list unaffected

sort() sorts the list in-place, mutating the list indices, and returns None

We cam use sorted on any itterable, list, dict, string, tupple.

Hence, use sorted(), when you want a sorted object back and sort() when u want to mutate the list.

As compare with sorted(), sort() is fast as it do not copy.

Read complete here : What is the difference between sorted(list) vs list.sort() ?

SOLUTION :

    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)
Community
  • 1
  • 1
Raj Damani
  • 782
  • 1
  • 6
  • 19
  • Your explanation is completely wrong. `sorted` isn't a method of any class at all, and what class these functions or methods are or aren't attached to doesn't dictate whether they operate in-place. – user2357112 Apr 17 '17 at 18:11
  • Any description about the answer and what's wrong in the question ❓ – Blasanka Apr 18 '17 at 04:41