Issue
I have a set I want to make into a sorted list. I run:
sorted_list=list(my_set).sort()
but this returns none
, even when both list(my_set)
and my_set
are both nonempty. On the other hand this:
sorted_list=list(my_set)
sorted_list.sort()
works just fine.
Why is this happening? Does python not allow methods to be called on objects directly returned by constructors?
Solution
.sort()
sorts the list in place and returns None
. You need to use the sorted()
function here.
>>> a = [3, 2, 1]
>>> print a.sort()
None
>>> a
[1, 2, 3]
>>> sorted(a)
[1, 2, 3]
Answered By - Sukrit Kalra
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.