[Python-il] Self containment

Shai Berger shai at platonix.com
Mon Nov 14 02:15:15 IST 2011


On Monday 14 November 2011, Rani Hod wrote:
> Dear Abby,
> 
> Any idea why sorted and list.sort behave differently in the following
> example?
> (specifically, why x is not sorted in the end?)
> 
> Thanks,
> R.
> 
> ------------8<--------------------8<--------
> 
> >>> x = ['one','two','three']; x.append(x)
> >>> sorted(x, key=len)
> 
> ['one', 'two', ['one', 'two', 'three', [...]], 'three']
> 
> >>> x.sort(key=len); x
> 
> [[...], 'one', 'two', 'three']
> ------------8<--------------------8<--------

As a hint, consider that a truly sorted x is

['one','two',['one','two',[...],'three'],'three']

Consider how that is different from sorted()'s output.

HTH,
	Shai.


More information about the Python-il mailing list