Python - IndexError on a program that takes data from an URL
Question: When I try to execute the program, I keep getting an IndexError: list index
out of range. Here is my code:

    
    
    ''' This program accesses the Bloomberg US Stock information page.
        It uses BeautifulSoup to parse the html and then finds the elements with the top 20 stocks.
        It finds the the stock name, value, net change, and percent change. 
    '''
    
    import urllib
    from urllib import request
    from bs4 import BeautifulSoup
    
    
    # get the bloomberg stock page
    bloomberg_url = "http://www.bloomberg.com/markets/stocks/world-indexes/americas"
    
    try:
        response = request.urlopen(bloomberg_url)
    except urllib.error.URLError as e:
        if hasattr(e, 'reason'):
            print('We failed to reach a server.')
            print('Reason: ', e.reason)
        elif hasattr(e, 'code'):
            print('The server couldn\'t fulfill the request.')
            print('Error code: ', e.code)
    else:
        # the url request was successful
        html = response.read().decode('utf8')
    
        # use the BeautifulSoup parser to create the beautiful soup object with html structure
        bloomsoup = BeautifulSoup(html, 'html.parser')
        pagetitle = bloomsoup.title.get_text()
    
        # the 20 stocks are stored in the first 10 "oddrow" tr tags
        #   and the 10 "evenrow" tr tags
        oddrows = bloomsoup.find_all("tr",class_="oddrow")
        evenrows = bloomsoup.find_all("tr",class_="evenrow")
    
        # alternate odd and even rows to put all 20 rows together
        allrows=[]
        for i in range(12):
            allrows.append(oddrows[i])
            allrows.append(evenrows[i])
        allrows.append(oddrows[12])
    
        # iterate over the BeautifulSoup tr tag objects and get the team items into a dictionary
        stocklist = [ ]
        for item in allrows:
            stockdict = { }
            stockdict['stockname'] = item.find_all('a')[1].get_text()
            stockdict['value'] = item.find("td",class_="pr-rank").get_text()
            stockdict['net'] = item.find('span',class_="pr-net").get_text()
            stockdict['%'] = item.find('td',align="center").get_text()
            stocklist.append(stockdict)
    
        # print the title of the page
        print(pagetitle, '\n')
    
        # print out all the teams
        for stock in stocklist:
            print('Name:', stock['stockname'], 'Value:', stock['value'], 'Net Change:', stock['net'],\
                'Percent Change:', stock['%'])
    


Answer: `oddrows` and `evenrows` only have 10 elements according to your comment.

> the 20 stocks are stored in the first 10 "oddrow" tr tags and the 10
> "evenrow" tr tags

But you loop 12 times instead of 10: `for i in range(12):`

Change 12 to 10 and it should work.

_Side note:_ I don't suggest hardcoding that value.

You could replace

    
    
    allrows=[]
    for i in range(12):
        allrows.append(oddrows[i])
        allrows.append(evenrows[i])
    

with

    
    
    allrows=[]
    for x,y in zip(oddrows,evenrows):
        allrows.append(x)
        allrows.append(y)
    

