469,336 Members | 5,643 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,336 developers. It's quick & easy.

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 15233
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
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Marylou17 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.