0

I am not sure why I am getting this list index out of bounds error

Basically what is supposed to happen is I am sending my def a list of twitter userIds and then breaking them into chunks of 100 looking them up in twitter, then adding them to a dictionary using the userIds as the key. So lets say 00001 is johnny we look up 00001 get johnny and then make a dictionary with 00001, johnny. However the if statements don't seem to trigger.

Here is the code:

 def getUserName(lookupIds):
     l = len(lookupIds) # length of list to process
     i = 0 #setting up increment for while loop 
     screenNames = {}#output dictionary
     count = 0 #count of total numbers processed
     print lookupIds
     while i < l:
         toGet = []
         if l - count > 100:#blocks off in chunks of 100
             for m  in range (0,100):
                toGet[m] = lookupIds[count]
                count = count + 1
                print toGet
         else:#handles the remainder 
              r = l - count 
              print screenNames
              for k  in range (0,r):#takes the remainder of the numbers 
                  toGet[k] = lookupIds[count]
                  count = count + 1
              i = l   # kills loop

          screenNames.update(zip(toGet, api.lookup_users(user_ids=toGet)))
          #creates a dictionary screenNames{user_Ids, screen_Names}

     #This logic structure breaks up the list of numbers in chunks of 100 or their
     #Remainder and addes them into a dictionary with their userID number as the 
     #index value Count is for monitoring how far the loop has been progressing.    
     print len(screenNames) + 'screen names correlated'
     return screenNames

The error is as follows:

Traceback (most recent call last):
  File "twitterBot2.py", line 78, in <module>
    toPrint = getUserName(followingids)#Testing Only
  File "twitterBot2.py", line 42, in getUserName
    toGet[k] = lookupIds[count]
IndexError: list assignment index out of range
1
  • 1
    try toGet.append(value) Commented Jun 17, 2014 at 1:39

3 Answers 3

1

toGet is initialized to the empty list, and you're attempting to assign [0] a value. This is illegal. Use append instead:

toGet.append(lookupIds[count])
Sign up to request clarification or add additional context in comments.

Comments

0

This is likely because you're attempting to lookup index zero when it doesn't exist. Example:

>>> x=[]
>>> x[0] = 1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range

Comments

0
def getUserName(lookUpIds):
    blockSize = 100
    screenNames = {}
    indexes = xrange(0, len(lookUpIds), blockSize)
    blocks = [lookUpIds[i:(i + blockSize)] for i in indexes]
    for block in blocks:
        users = api.lookup_users(user_ids=block)
        screenNames.update(zip(block, users))

    return screenNames

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.