Python - Understanding error: IndexError: list index out of range

16,953

Solution 1

Using range for iteration is nearly always not the best way. In Python you can iterate directly over a list, dict, set etc.:

for item in d.entries:
    updates.append([{"url": feed_a_enviar["linktoourpage"], "msg": item.title + ", "}])

Obviously d.entries[i] triggers the error because that list contains less than 8 items (feeds_updates may contain 8, but you are not iterating over that list).

Solution 2

d.entries has less than 8 elements. Iterate over d.entries directly instead of some disconnected range.

Share:
16,953
André
Author by

André

Updated on November 20, 2022

Comments

  • André
    André over 1 year

    I'm fairly new to python. I have an error that I need to understand.

    The code:

    config.py:

    # Vou definir os feeds
    feeds_updates = [{"feedurl": "http://aaa1.com/rss/punch.rss", "linktoourpage": "http://www.ha.com/fun.htm"},
                     {"feedurl": "http://aaa2.com/rss.xml", "linktoourpage": "http://www.ha.com/fun.htm"},
                     {"feedurl": "http://aaa3.com/Heaven", "linktoourpage": "http://www.ha.com/fun.htm"},
                     {"feedurl": "http://aaa4.com/feed.php", "linktoourpage": "http://www.ha.com/fun.htm"},
                     {"feedurl": "http://aaa5.com/index.php?format=feed&type=rss", "linktoourpage": "http://www.ha.com/fun.htm"},
                     {"feedurl": "http://aaa6.com/rss.xml", "linktoourpage": "http://www.ha.com/fun.htm"},
                     {"feedurl": "http://aaa7.com/?format=xml", "linktoourpage": "http://www.ha.com/fun.htm"},
                     {"feedurl": "http://aaa8/site/component/rsssyndicator/?feed_id=1", "linktoourpage": "http://www.ha.com/fun.htm"}]
    

    twitterC.py

    # -*- coding: utf-8 -*-
    import config   # Ficheiro de configuracao
    import twitter
    import random
    import sqlite3
    import time
    import bitly_api #https://github.com/bitly/bitly-api-python
    import feedparser
    
    ...
    
    # Vou escolher um feed ao acaso
    feed_a_enviar = random.choice(config.feeds_updates)
    # Vou apanhar o conteudo do feed
    d = feedparser.parse(feed_a_enviar["feedurl"])
    # Vou definir quantos feeds quero ter no i
    i = range(8)
    print i
    # Vou meter para "updates" 10 entradas do feed
    updates = []
    for i in range(8):
        updates.append([{"url": feed_a_enviar["linktoourpage"], "msg": d.entries[i].title + ", "}])
    # Vou escolher ums entrada ao acaso
    print updates # p debug so
    update_to_send = random.choice(updates)
    
    print update_to_send # Para efeitos de debug
    

    And the error that appears sometimes because of the nature of the random:

    Traceback (most recent call last):
      File "C:\Users\anlopes\workspace\redes_sociais\src\twitterC.py", line 77, in <module>
        updates.append([{"url": feed_a_enviar["linktoourpage"], "msg": d.entries[i].title + ", "}])
    IndexError: list index out of range
    

    I'am not getting to the error, the list "feeds_updates" is a list with 8 elements, I think is well declareted and the RANDOM will choose one out of the 8...

    Can someone give me a clue on what is happenning here?

    PS: Sorry for my bad english.

    Best Regards,

    • user1066101
      user1066101 about 13 years
      You are aware that indexes start at zero? 8 elements have indexes 0 to 7?
    • Tim Pietzcker
      Tim Pietzcker about 13 years
      @S. Lott: And range(8) is [0,1,2,3,4,5,6,7], so that's not the problem here.
    • user1066101
      user1066101 about 13 years
      @Tim Pietzcker: Of course, we're both assuming that the code which was presented actually has 8 items in the feed_updates list, aren't we? Yes, the evidence is good, but there has to be something going on that's not shown in the code snippet provided.
    • Tim Pietzcker
      Tim Pietzcker about 13 years
      @S. Lott: The number of items in feed_updates is irrelevant because he's iterating over a completely different list. See my answer.