Mario
Mario

Reputation: 3

how to reduce the time of a nested loop in python

I am getting the player data from ESPN, but I find myself with the problem that to get each variable the waiting time is very long, how could I improve the efficiency?

players_by_temp = []
for i in range(20):
    players = []
    for j in range(len(html_table[i].find_all(class_='AnchorLink'))):
        players.append(html_table[i].find_all(class_='AnchorLink')[j].text)
    players_by_temp.append(players)
    print(i)

Upvotes: 0

Views: 87

Answers (1)

Frank Yellin
Frank Yellin

Reputation: 11332

players_by_temp = []
for i in range(20):
    players = []
    for anchor in html_table[i].find_all(class_='AnchorLink'):
        players.append(anchor.text)
    players_by_temp.append(players)
    print(i)

Once you get more comfortable in Python, you would then replace the three center lines with the following:

    players = [anchor.text for anchor in html_table[i].find_all(...)

Upvotes: 1

Related Questions