473,657 Members | 2,496 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 15590
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.co mwrote:
>
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.com wrote:
>
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********@gma il.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
13742
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 NoneType object, that I have many problems to use :-( Did I miss somthing ? Any clue ?
1
13081
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 statements calling the class objects in my extention DLL. 3. Before each execution I import my extention module using;
8
2871
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. This is undesirable in cases where I would like to circumvent execution of code meant to parse out data from my finditer() object. I know that if I place a finditer() object in an iterative for loop, the loop will not execute, but is there some...
1
11158
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 not callable"
7
53011
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 case of iterable, doesn't consume the first element of the iterable) Regards,
0
1881
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 attempt to load a module at runtime and then request an object of that type. Essentially, my code works as follows: I have a registry class which has a method to register a class. It
9
24037
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 I try to run it. I've already posted at ESRI support, and I was hoping that Python people could help me more. I hope there is something simple I could do to be able to define the object that it thinks is NoneType. Please when someone responds,
13
3040
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 attribute 'getID'
2
7452
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 error messages: Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'NoneType' object is not callable (the exception raised is a TypeError; NoneType is part of the message).
0
8325
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8844
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8742
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7354
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5643
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2743
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1971
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1734
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.