472,132 Members | 1,383 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,132 software developers and data experts.

NoneType object not iterable

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
5 15482
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
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
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
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
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.

Similar topics

2 posts views Thread by Arnaud-F. FAUSSE | last post: by
1 post views Thread by Atul Kshirsagar | last post: by
1 post views Thread by homepricemaps | last post: by
7 posts views Thread by Tim N. van der Leeuw | last post: by
reply views Thread by John Allman | last post: by
9 posts views Thread by thompson.marisa | last post: by
13 posts views Thread by globalrev | last post: by
2 posts views Thread by Gabriel Genellina | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.