0

Can anyone tell me why sorting is not working here?

    b = input()
    list1  = input().split()
    c = input() 
    list2 = input().split()
    set1 = set(list1)
    set2 = set(list2)
    list3 = list(set1.union(set2) - set1.intersection(set2))
    sorted(list3)
    print(list3)

in put format :

4

2 4 5 9

4

2 4 11 12

I_code
  • 59
  • 3
  • 11

2 Answers2

1

sorted does not sort in place. Use

list4 = sorted(list3) 

and print that. See f.e. here: https://www.programiz.com/python-programming/methods/built-in/sorted

Return value from sorted() sorted() method returns a sorted list from the given iterable.

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
0

sorted(list3) returns a new list.

Either assign it to a variable

list3 = sorted(list3)

or use the sort method to change the list in-place.

list3.sort()

For more on Python sorting, have a look at Sorting HOW TO from the official Python documentation

Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96
  • That too is not working, output is still ['11', '12', '5', '9']! – I_code Nov 26 '17 at 14:26
  • 1
    @Nexus because you want to make them integers first, no? The sorting is correct for strings... – Jon Clements Nov 26 '17 at 14:28
  • 2
    @Nexus Those are sorted strings. If you expect them to be integers, then make them integers beforehand: `list3 = [int(x) for x in list3]` Otherwise, you can use a key function to sort them as if they were `int`s. `list3.sort(key=int)` – Patrick Haugh Nov 26 '17 at 14:28
  • 1
    @Nexus: see, you didn't explain very well that you have *string values*. Use `sorted(list3, key=int)` then. – Martijn Pieters Nov 26 '17 at 14:29
  • Yes! That explains ! How did I miss this silly point ! :( Thanks @JonClements – I_code Nov 26 '17 at 14:31