-2

I am writing a program to take a list of words from the user, remove the repeated words and then print the output after sorting the words. However, I ran into this issue while sorting:

a = set(input('Words: ').split(' '))
b = list(a).sort()

print(b)

The terminal prints 'None' as the result of the code, but in this case:

a = set(input('Words: ').split(' '))
b = list(a)
b.sort()
print(b)

It gives the right output. Why is this so?

  • 1
    The `sort()` function sorts the list inplace it doesn't return anything. So in the first case, b would contain `None`. However you could use the `sorted()` function to correct the first code. – Suraj Sep 19 '20 at 18:30

1 Answers1

0

The difference between item.sort() and sorted(item) is explained (when to use which) in detail here : What is the difference between `sorted(list)` vs `list.sort()`?

hack3r-0m
  • 700
  • 6
  • 20