badc0re
badc0re

Reputation: 3523

python for loop extending

I wonder why the for loop is not extending it's iteration:

    for link in frontLinks:  
        #try:           
        getCurlink = self.getHref(link) # get current site links
        #print getCurlink
        #print frontLinks 
        if getCurlink:
            frontLinks = frontLinks + getCurlink

This line:

frontLinks = frontLinks + getCurlink

doesn't apply to frontLinks of the "for" loop. Any ideas??

Upvotes: 0

Views: 1700

Answers (3)

eumiro
eumiro

Reputation: 212885

Although you can append to a list while iterating, I would use one list and a deque (you can use a list instead of deque, just replace todo.popleft() with todo.pop(0)):

from collections import deque

done = []
todo = deque(frontLinks) # your initial frontLinks
while todo:
    link = todo.popleft() # take the first element from todo
    getCurlink = self.getHref(link) # get current site links
    if getCurlink:
        todo.extend(getCurlink) # extend the todo list
    done.append(link)

This way you always have two collections with clear roles: todo and done. You can stop/resume the iteration, dump/load the current state, etc. without having to restart the whole process.

Upvotes: 1

Igor Chubin
Igor Chubin

Reputation: 64563

You will not see changes in a list while iterating over it.

You need to use something like:

 while i < len(frontLinks):
     link = frontLinks[i]
     ...
     if condition:
       frontLinks.append(item)
     i += 1

Upvotes: 1

Ned Batchelder
Ned Batchelder

Reputation: 375574

The for loop evaluates its expression once, to get an iterator. Later, you re-bind the name frontLinks to be a new list. The new list won't have anything to do with the for loop.

Although it is tricky to modify a list while iterating over it, it's OK to add to the end of the list, it will work. Change your last line to this:

frontLinks.extend(getCurlink)

Upvotes: 7

Related Questions