Dictionaries

I’m having a problem iterating over python dictionaries. I can create dictionaries and access items via their keys, but if I run the following code, it fails on lines 6 and 9 on a “non-sequence” error, although it works in my other Python idle environment:

testDict = {}
testDict['test1'] = [1,2,3]

print testDict['test1'][2]

for key in testDict:
	print key

if 'test1' in testDict:
	print 'found'

I’m guessing that it has something to do with the version of Jython that you use. Is there a way around this? Do I have to import something first? All I want to do is see if a key already exists or not.

To check whether a key exists or not simply usetestDict.has_key('test1')This will return true or false (1 or 0).

If you wanted to iterate through a dictionary, you would usefor key, value in testDict.items(): print key, valueIf you just used for key in testDict.items(): print key‘key’ would contain a tuple like ('test1', [1, 2, 3])
Al

Thanks Al, that was it. Apparently, Jython didn’t have a built in iterator until V2.2, so the method call is a little different. In later versions, you can just do a “for key in dict” and “if key in dict” directly.

Thanks again.