Python 3 sort() problem

Mark Lawrence breamoreboy at yahoo.co.uk
Mon Aug 17 10:18:45 EDT 2015


On 17/08/2015 12:42, Владислав wrote:
> # first: works fine
>
> x = [1, 2, 4, 2, 1, 3]
> x = list(set(x))
> x.sort()
> print(x) /# output: 1, 2, 3, 4
>
> /# second: why x became None ??
>
> x = [1, 2, 4, 2, 1, 3]
> x = list(set(x)).sort()
> print(x) /# output: None/
>
> I know that sort() returns None, but I guess that it would be returned x
> that was sorted. Why so?/

A set is created from x.  This is converted to a list.  You call sort() 
and assign the return value from that, None, to x.  You will see exactly 
the same thing above if you do:-

x = x.sort()

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence




More information about the Python-list mailing list