By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,857 Members | 1,760 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,857 IT Pros & Developers. It's quick & easy.

NoneType object not iterable

P: n/a
tom
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...
Jul 13 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
to*@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
Jul 13 '07 #2

P: n/a
On Fri, 13 Jul 2007 20:44:13 +0300, <to*@finland.comwrote:
>
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...
db is out of scope, you have to pass it to the function:
>def display(db):
keyList = db.keys()
sortedList = keyList.sort()
for name in sortedList:
line = 'Name: %s, Number: %s' % (name, db[name])
print line.replace('\r', '')

--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
Jul 13 '07 #3

P: n/a
Daniel wrote:
db is out of scope, you have to pass it to the function:
What's wrong about module attributes?

Regards,
Björn

--
BOFH excuse #418:

Sysadmins busy fighting SPAM.

Jul 13 '07 #4

P: n/a
On Fri, 13 Jul 2007 21:13:20 +0300, Bjoern Schliessmann
<us**************************@spamgourmet.comwrote :
>
Daniel wrote:
>db is out of scope, you have to pass it to the function:

What's wrong about module attributes?
I made a mistake
Jul 13 '07 #5

P: n/a
Stargaming <st********@gmail.comwrites:
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.
Alternatively, use
sortedList = sorted(keyList)
Jul 13 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.