4 Replies - 1055 Views - Last Post: 04 September 2012 - 06:39 AM Rate Topic: -----

#1 NunoSantos  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 04-September 12

list index out of range

Posted 04 September 2012 - 02:17 AM

hi people...i'm new in here.
i'm portuguese, so sorry my english.
iḿ new programming in python so

this is my code:
 for i in range(1, len(root)):
                    if dtResult.has_key(root[i]):                        
                        if dtResult[root[i]].has_key(path[i]):
                            dtResult[root[i]][path[i]] += 1
                        else:
                            dtTemp = {}
                            dtTemp[path[i]] = 1
                            dtResult[root[i]].update(dtTemp)
                            
                    else:
                        dicTemp = {}
                        dicTemp[path[i]] = 1
                        dtResult [root[i]]=dicTemp 


so i have an error: list index out of range on line 3 and i dont know why...
any body can give me a hand?

thanks

MOD EDIT: Added code tags. When posting code...USE CODE TAGS!!! ESPECIALLY IN PYTHON!

:code:

This post has been edited by JackOfAllTrades: 04 September 2012 - 03:38 AM


Is This A Good Question/Topic? 0
  • +

Replies To: list index out of range

#2 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: list index out of range

Posted 04 September 2012 - 04:08 AM

I think your problem is that list indices start at 0, and not at 1!

for i in range(1, len(root)):


A possible fix would be to go from 0 to len(root):

for i in range(0, len(root)):


Or if you wanted to start from the second element 1 to len(root)-1 which would still be in range:

for i in range(1, len(root)-1):

This post has been edited by Simown: 04 September 2012 - 05:28 AM

Was This Post Helpful? 1
  • +
  • -

#3 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2091
  • View blog
  • Posts: 3,185
  • Joined: 21-June 11

Re: list index out of range

Posted 04 September 2012 - 05:31 AM

It would help if you could find out which of the accesses on line 3 is out of bounds (for example by moving each one on its own line and seeing which line it complains about now). From the looks of it, I'd say the problem would have to be path[i]. Since the upper bound for i is len(root), indexing root can't be the problem, and you're also checking whether the key is valid for dtResult, so that can't be the problem either. So the only thing left is path.

Apparently you assume that path is as long as root when, in fact, it's not.

View PostSimown, on 04 September 2012 - 01:08 PM, said:

I think your problem is that list indices start at 0, and not at 1!

for i in range(1, len(root)):


That won't cause i to go out of the bounds of root though. It will just skip the first element.
Was This Post Helpful? 1
  • +
  • -

#4 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: list index out of range

Posted 04 September 2012 - 05:42 AM

True, true. You're totally correct - I misread it slightly. It's still quite likely that you meant to start from 0 and not 1 anyway, a common beginners mistake.

It's more likely that path has a different length from root, as you say it goes out of range on line 3, the first instance of path[i]. Remember it's using the same i to access root and path, so I'd agree that the problem lies there.

This post has been edited by Simown: 04 September 2012 - 01:11 PM

Was This Post Helpful? 0
  • +
  • -

#5 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7578
  • View blog
  • Posts: 12,742
  • Joined: 19-March 11

Re: list index out of range

Posted 04 September 2012 - 06:39 AM

The obvious diagnostic check would be to
  print len (root), len (path)



at the start of that method. If the former is greater than the latter, you're going out of bounds.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1