6 Replies - 7852 Views - Last Post: 28 April 2011 - 12:49 PM Rate Topic: -----

#1 novman  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 41
  • Joined: 03-June 10

list index out of range

Posted 28 April 2011 - 04:09 AM

Hey guys,
This probly a really easy fix. I know this exception occurs when you try and access an index which does not exist, but
this code worked fine then just started giving this error and i cant figure out why...

cur.execute('select first, last from users where email = ?', [email])
    userlist = []
    for user in cur.fetchall():
        userlist.append( (user[0] + " " + user[1]) )
        #dummy additions to the list...
        userlist.append( (user[0]) )
        userlist.append( (user[1]) )

    #print out the number of items in userlist - should be 3
    use = len(userlist)
    print "Content-Type: text/html\n"
    print use
      
    rendered = tpl % (userlist[0])
    return rendered
    



Errors:
<type 'exceptions.IndexError'>: list index out of range
args = ('list index out of range',)
message = 'list index out of range'

Is This A Good Question/Topic? 0
  • +

Replies To: list index out of range

#2 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: list index out of range

Posted 28 April 2011 - 05:53 AM

Could you please copy and paste the exact error? I really can't do much with this.
Was This Post Helpful? 0
  • +
  • -

#3 novman  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 41
  • Joined: 03-June 10

Re: list index out of range

Posted 28 April 2011 - 11:50 AM

util.py in display_name(email=None)
147 #print out the error
148
149 rendered = tpl % (userlist[0])
150 return rendered
151
rendered undefined, tpl = '%s', userlist = []

<type 'exceptions.IndexError'>: list index out of range
args = ('list index out of range',)
message = 'list index out of range'
Was This Post Helpful? 0
  • +
  • -

#4 Nallo  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 163
  • View blog
  • Posts: 255
  • Joined: 19-July 09

Re: list index out of range

Posted 28 April 2011 - 12:19 PM

If your sql statement doesn't find something to grab from the datebase cur.fetchall() will be empty and so will be userlist.
so it is no surprise that in that case rendered = tpl % (userlist[0]) throws a list index out of range error. You cannot get the first element of an empty list.

This post has been edited by Nallo: 28 April 2011 - 12:20 PM

Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5775
  • View blog
  • Posts: 12,587
  • Joined: 16-October 07

Re: list index out of range

Posted 28 April 2011 - 12:20 PM

Just a shot in the dark.

This doesn't look right:
cur.execute('select first, last from users where email = ?', [email])



Try:
cur.execute('select first, last from users where email = ?', (email,) )



Most DB implementations I've seen have a thing for tuples.
Was This Post Helpful? 0
  • +
  • -

#6 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: list index out of range

Posted 28 April 2011 - 12:22 PM

for future reference let me show you an example of what I'm looking for

Here's my code:
def test():
    x = []
    x.pop(4)
test()



Here's my error:
Traceback (most recent call last):
  File "C:/Users/adam/Desktop/test.py", line 4, in <module>
    test()
  File "C:/Users/adam/Desktop/test.py", line 3, in test
    x.pop(4)
IndexError: pop from empty list


I literally copied and pasted that from IDLE. See how it tells me what function and even what line caused the error? That's what will help people help you!!!
Was This Post Helpful? 0
  • +
  • -

#7 Nallo  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 163
  • View blog
  • Posts: 255
  • Joined: 19-July 09

Re: list index out of range

Posted 28 April 2011 - 12:49 PM

@baavgai:
I don't think so. Novman said his code worked before. So the problem is more likely to happen if it runs into an edge case. Which I think it does, see my post above.

@atraub:
I understand what you preach here. But maybe novman is using some obscure code base here that catches errors/exceptions and print out their own crummy error message. Can't blame him for only providing that. Though I would blame the creator of such a code base :gun_bandana:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1