473,395 Members | 1,466 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 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 15579
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Arnaud-F. FAUSSE | last post by:
Hello, I wrote code to store and fetch information from MySQL, and I have this problem: - using .fetchall(), I get tuples (correct according to specification) - using .fetchone(), I get...
1
by: Atul Kshirsagar | last post by:
Hello, I am using Python 2.3.2 with a C++ extention DLL in muti-threaded environment. 1. For each new thread I create a separate sub-interpreter. 2. Each thread executes multiple python...
8
by: Chris Lasher | last post by:
Hello, I really like the finditer() method of the re module. I'm having difficulty at the moment, however, because finditer() still creates a callable-iterator oject, even when no match is found....
1
by: homepricemaps | last post by:
if i do the following i get the url of an image i am looking for image = "" image = bs.img print image however if i do this out.write (image ) i get an error that says "nonetype error is...
7
by: Tim N. van der Leeuw | last post by:
Hi, I'd like to know if there's a way to check if an object is a sequence, or an iterable. Something like issequence() or isiterable(). Does something like that exist? (Something which, in...
0
by: John Allman | last post by:
Hi all, I'm trying to create a setup which allows a program to request an object using strings and get an object of that type. It appears to be mostly working but i have difficulties if i...
9
by: thompson.marisa | last post by:
Hi. I'm extremely new to Python and programming as a whole. I have written a python script with the assistance of ESRI ArcGIS 9.2, which uses Python 2.4.1, however, it gives me this error when...
13
by: globalrev | last post by:
wassup here? 7 Traceback (most recent call last): File "C:\Python25\myPrograms\netflix\netflix.py", line 22, in <module> print cust1.getID() AttributeError: 'NoneType' object has no...
2
by: Gabriel Genellina | last post by:
En Sun, 15 Jun 2008 05:35:18 -0300, Maryam Saeedi <ma.saeedi@gmail.comescribió: NoneType is not an exception, but the type of the None object. Perhaps you're not interpreting correctly some...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.