Q:

Looping stops after the first item in the list

places = []
persons = []    
unknown = []
newlist = []
filename = 'file.html' 
tree = etree.parse(filename)
input_file = open(filename, 'rU')
def extract(tree):   
     <some code>
    return places
    return persons
    return unknown

def change_class():
 extract(tree)  

 for line in input_file:        
    for x in places:
         for z in unknown:                                                                       

            if x+'</dfn>' in line:

                    newline = line.replace('"person"', '"place"')
                    newlist.append(newline)

            elif z+'</dfn>' in line:

                    newline = line.replace('"person"','"undefined"')
                    newlist.append(newline)
            else:
                newlist.append(line)

            break
         break

 for x in newlist:
    print x

I have an html-file of this kind with erroneous class values:

 <html>
  <head></head>
  <body>
    <p class ='person'><dfn>New-York</dfn>
    <p class = 'place'><dfn>John Doe</dfn>
    <p class ='person'><dfn>Paris</dfn>
    <p class = 'place'><dfn>Jane Doe</dfn>
  </body>
</html>

My script allows me to reprint the same file, but it replaces the class value only for the first item of both lists (places and unknown):

 <html>
  <head></head>
  <body>
    <p class ='place'><dfn>New-York</dfn>
    <p class = 'unknown'><dfn>John Doe</dfn>
    <p class ='person'><dfn>Paris</dfn>
    <p class = 'place'><dfn>Jane Doe</dfn>
  </body>
</html>

Then it kinda stops iterating over both lists and goes directly to the else-step and adds all the rest to the newlist without replacements. Python yelds no errors, list are successfully extracted with the extract() function as well, I checked... 

A:

known_places = #list of known places
unkowns = #list of unknown places and persons

newlist = []
for line in input_file:
    if any(place in line for place in Known_places):
        line = line.replace("person", "place")
    elif any(unkown in line for unkown in unkowns):
        line = line.replace("person","undefined")
    newlist.append(line)

Something like this might work. 

