NoneType object not iterable

Stargaming stargaming at gmail.com
Fri Jul 13 19:56:21 CEST 2007


tom at finland.com schrieb:
> Hi!
> My code is
> 
>  > db = {}
>  >
> 
>> def display():
>>     keyList = db.keys()
>>     sortedList = keyList.sort()
>>     for name in sortedList:
>>         line = 'Name: %s, Number: %s' % (name, db[name])
>>         print line.replace('\r', '')
> 
> 
> And it gives following error:
> 
>>     for name in sortedList:
>> TypeError: 'NoneType' object is not iterable
> 
> 
> How can sortedList variable turn into NoneType? I just don't get it...

It does not turn into something. The `sort()` method just works "in 
place", i. e. it will mutate the list it has been called with. It 
returns None (because there is no other sensible return value).

For you, that means: You don't have to distinguish between keyList and 
sortedList. Just call ``.sort()`` on keyList and it will just work.

HTH,
Stargaming

P.S. same information is in the docs: 
http://docs.python.org/lib/typesseq-mutable.html



More information about the Python-list mailing list