Python 2.4 and list of dictionary issues

Chris Rebert clp2 at rebertia.com
Mon Feb 8 00:35:40 EST 2010


On Sun, Feb 7, 2010 at 9:08 PM, Chris Stevens <cjstevens at gmail.com> wrote:
> Hi all,
>
> I'm a python newbie so please excuse me if I am missing something
> simple here. I am writing a script which requires a list of
> dictionaries (originally a dictionary of dictionaries, but I changed
> it to a list to try and overcome the below problem).
>
> Now my understanding is that you create the empty list, then append or
> add entries to it. Correct?
>
> Here is some code:
>
> userInfo = []
> ...
> userInfo.append( {
>                                                'username' : uidString[0],
>                                                ...
>                                                'failedattempts' : int(0)
> })
>
> I'm not to sure on the bracketing here, but I have tried many
> combinations. The issue here is that I get a "IndexError: list index
> out of range" message on the line "userInfo.append( {"

Please include the full and exact error Traceback, and remember to do
so in the future.

> I wrote this script on a box with Python 2.6 and it worked fine.
> Moving it to a box with 2.4, and I get this error. I can't understand
> why i'm getting a list index out of range error when trying to append
> (not reference) a list?? I have also tried "+=", and
> userInfo(len(userInfo))= .... just get the same error.
>
> Could anyone shed some light on this?

I would guess that `uidString` is probably an empty string or list,
thus when you try to access the first character or item of it via
uidString[0], you get an error. However, as you didn't include a
Traceback, I can't be sure. I have no idea why the error only shows
itself for certain Python versions; the problem is probably in some
part of the code you're not showing us.

Cheers,
Chris
--
http://blog.rebertia.com



More information about the Python-list mailing list