3

I am starting to learn Python.

Can someone explain why sort() returns None?

alist.sort()            ## correct
alist = blist.sort()    ## NO incorrect, sort() returns None

Why shouldn't

alist = blist.sort()

return the sorted list and give it back to alist? This does not make sense to me.

Thanks.

2
  • 1
    sorted('FMRT', key=lambda x: ord(x) % 16) Commented Feb 23, 2012 at 6:30
  • 1
    Did you try reading the documentation? Commented Feb 23, 2012 at 6:46

3 Answers 3

11

alist.sort() sorts alist in-place, modifying alist itself.

If you want a new list to assign somewhere, use blist = sorted(alist)

Sign up to request clarification or add additional context in comments.

Comments

0

Use the following:

alist = sorted(blist)

Comments

0

When you want to perform the sorting on same list then you have to use sort() method of list. But If you dont want to change the sequence of original list but you need a sorted copy of the original list then use sorted() inbuilt function.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.