a question regarding conciseness

Jason caljason76 at yahoo.com
Wed Feb 20 17:42:12 EST 2002


sort() does not return the sorted list, it returns None.  sort() does
an inplace sort and is not a fuctional construct; it does not generate
a new sorted list.

> However when I use:
> 
> for i in d.keys().sort():
>   do something
> 
> I get:
> 
> Traceback (innermost last):
>   File "<stdin>", line 1, in ?
> TypeError: loop over non-sequence
> 
> But I know that d.keys() returns a list to which the sort() function should 
> be applicable.
> 
> When I do:
> 
> k = d.keys()
> k.sort()
> for i in k:
>   do something
> 
> it works fine.
> Why can't I use d.keys().sort() directly? And is there any other more 
> concise (elegent?) way to loop over sorted dictionary keys?
>



More information about the Python-list mailing list