Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

dictionary error: list assignment index out of range

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


vaduvoiutibi at yahoo

Jan 27, 2011, 11:34 PM

Post #1 of 3 (768 views)
Permalink
dictionary error: list assignment index out of range

Hy everyone, I'm trying to learng python for a week or two and there's a thing
that is really disturbing me as I do not understand what the problem is. I'm
trying to use a dictionary to remember when a user has visited a city. Code is
really basic:

in the class init method I added
self.visited = []

and in the function where i check if city was visited:
cityNumber = 1 #example
if (not cityNumber in self.visited):
#do some stuff
self.visited[cityNumber] = "true"

Apparently the last line causes the error: list assignment index out of range. I
read that this is the simplest way to assign a value to a
dictionary(dict[key]=value). So why is the error appearing?? Thanks a lot in
advance


vaduvoiutibi at yahoo

Jan 27, 2011, 11:39 PM

Post #2 of 3 (735 views)
Permalink
Re: dictionary error: list assignment index out of range [In reply to]

Well, to quote firefox: this is embarrassing. I've realized the dictionary
initialization is wrong, as [] means its a tuple, I should use {}. That's why I
don't like working nights..it's only in the morning when you start seeing things
better. I apologize for the mail. Cheers





________________________________
From: Vaduvoiu Tiberiu <vaduvoiutibi [at] yahoo>
To: python-list [at] python
Sent: Fri, January 28, 2011 9:34:57 AM
Subject: dictionary error: list assignment index out of range


Hy everyone, I'm trying to learng python for a week or two and there's a thing
that is really disturbing me as I do not understand what the problem is. I'm
trying to use a dictionary to remember when a user has visited a city. Code is
really basic:

in the class init method I added
self.visited = []

and in the function where i check if city was visited:
cityNumber = 1 #example
if (not cityNumber in self.visited):
#do some stuff
self.visited[cityNumber] = "true"

Apparently the last line causes the error: list assignment index out of range. I
read that this is the simplest way to assign a value to a
dictionary(dict[key]=value). So why is the error appearing?? Thanks a lot in
advance


orasnita at gmail

Jan 28, 2011, 12:43 AM

Post #3 of 3 (764 views)
Permalink
Re: dictionary error: list assignment index out of range [In reply to]

From: Vaduvoiu Tiberiu
> Well, to quote firefox: this is embarrassing. I've realized the dictionary initialization is wrong, as [] means its a tuple, I should use {}. That's why I > don't like working nights..it's only in the morning when you start seeing things better. I apologize for the mail. Cheers


[] is for lists.
() is for tuples.

HTH.

Octavian

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.