list doubt. | Sololearn: Learn to code for FREE!

+1

list doubt.

Hello teacher, I tried a code for the below problem statement. (Write a program that accepts a comma-separated sequence of words as input and prints the words in a comma-separated sequence after sorting them alphabetically.) My code is below .But I got None as output. code: words=input("Enten words:") a=words.split(",") b=a.sort() print(b) But again I wrote the code as below. And I got an answer. words=input("Enten words:") a=words.split(",") a.sort() print(a) so could you please tell me what is difference between these codes? And why it does not give me the output in 1st program.

4/30/2021 9:18:16 AM

Gouri Shinde

2 Answers

New Answer

+12

sort() does sort the list in place and returns only None. a.sort() alters the list a itself. If you want the list a to be unchanged and you would like to put the sorted items of list a somewhere else, you would have to use sorted().

+6

sort in an inplace sort.it returns None.So b is None and a is sorted. in opposit sorted(a) returns a new list. a.sort() print(a)