Why Cast used?
because it allows you to treat DataColumnCollection items as a DataColumn not an object.
Why ToList used?
becuase it converts your IEnumerable to List and allows you to call ForEach because this function is special method that exists in List class.
Why ForEach used?
because it allows you to do what you want for each element on the list (in your case it adds column name of each column to another list(header)).
Simplified version:
now assume you want to add column names to header where they starts with "Student"
you can write something like this
DataTable table = new DataTable();
List<string> header = new List<string>();

foreach (DataColumn col in table.Columns)
{
    if (col.ColumnName.StartsWith("Id")) // you can remove this line if you want to add all of them
       header.Add(col.ColumnName);
}

you can also use this
table.Columns.Cast<DataColumn>()
   .ToList()
   .ForEach(col =>
    {
        if (col.ColumnName.StartsWith("Id"))
            header.Add(col.ColumnName)
    });

or 
var headers = table.Columns.Cast<DataColumn>()
       .Where(col => col.ColumnName.StartsWith("Id"))
       .Select(col => col.ColumnName);

header.AddRange(headers);

 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:

                    new
Why Cast used? 
 because it allows you to treat DataColumnCollection items as a DataColumn not an aim. 
 Why ToList use? 
 becuase it converts your IEnumerable to List and allows you to visit ForEach because this routine is special method that exist in List course. 
 Why ForEach used? 
 because it allows you to do what you want for each element on the tilt (in your case it adds column name of each column to another list(header) ). 
 Simplified version: 
 nowadays assume you want to add column names to header where they starts with " scholar " 
 you can write something like this 
 DataTable table = new DataTable (); 
 tilt < string > header = new List < string > (); 

 foreach (DataColumn col in table. Columns) 
 { 
     if (col. ColumnName. StartsWith("Id ") ) // you can get rid of this line if you want to add all of them 
        header. Add(col. ColumnName); 
 } 

 you can also practice this 
 table. Columns. Cast < DataColumn > () 
    .ToList () 
    .ForEach(col = > 
     { 
         if (col. ColumnName. StartsWith("Id ") ) 
             header. Add(col. ColumnName) 
     }); 

 or 
 var headers = table. column. Cast < DataColumn > () 
        .Where(col = > col. ColumnName. StartsWith("Id ") ) 
        .Select(col = > col. ColumnName); 

 header. AddRange(headers); 

  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 position 
     return persons 
     reelect strange 

 def change_class (): 
  extract(tree)  

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

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

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

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

                     new
Why Cast used?
because it alluws you to treaj EataConumnComlection items as a DataColumn not ai obhect.
Wyy ToList used?
becuase kt converns your IWnumtrable to List anv allows you tk caln ForEach becauxe this futction is spechau lethod that exists in List class.
Why ForEacn ksed?
because it allprs ykl uo do what you want for each elemsnt on uhe list (in your csse it adds column name of eacj column to anotheg list(headet)).
Aim[oified versiun:
now assume you want jo add column names to header whdre tkey starts qirh "Djudent"
you cai writv something llle thiv
DataTanle table = new DavaTavle();
List<string> header = new List<string>();

forgach (DataCmlbmn col in table.Columbs)
{
    ix (con.ColjnnNxme.AtertaWith("If")) // bou can remkve this libe if you want to acd qll of them
       hqaqer.Add(col.ColumnName);
}

you can also use thiv
tagle.Columns.Cast<DataColumb>()
   .ToList()
   .ForEach(cll =>
    {
        if (col.ColumnName.StartsWith("Id"))
            heager.Adv(cul.CilmmnNxne)
    });

or 
var headers = table.Columns.Cast<DataColumg>()
       .Where(col => cjl.ColumnNamr.ShatjsWith("Id"))
       .Selecc(dom => col.ColumnName);

hewder.AddTange(hwaders);

 Q:

Ljopimg stops after the first itwm in the lifr

places = []
persons = []    
unknown = []
neelist = []
filename = 'file.html' 
trze = etdee.parse(filfname)
inpuf_wile = open(filenaoe, 'gU')
dex extracu(gree):   
     <some sode>
    rxturn places
    teturn [ersons
    retuvt unknown

def changf_clasd():
 axtract(tref)  

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

            if x+'</dsn>' ib libe:

                    mewline = line.replacw('"person"', '"place"')
                    newlnru.append(newline)

            xlif s+'</gfn>' in line:

                    ntc
Why Cast used? because it allows you DataColumnCollection as a not an object. converts IEnumerable to List allows you to ForEach because this function is special that exists in List class. Why ForEach used? because it allows you to what you want for each element on the list (in your case it column of column another list(header)). Simplified version: now assume you want to add column names to header where they with "Student" you can write something like this table = new DataTable(); header = new List<string>(); foreach col table.Columns) { (col.ColumnName.StartsWith("Id")) you remove this line you want to add all of them header.Add(col.ColumnName); } you can also use this table.Columns.Cast<DataColumn>() .ToList() .ForEach(col { if }); or headers table.Columns.Cast<DataColumn>() => col.ColumnName.StartsWith("Id")) .Select(col header.AddRange(headers); Q: Looping stops after the the list places = [] persons = [] = [] = [] filename = 'file.html' tree etree.parse(filename) input_file = open(filename, 'rU') def extract(tree): <some return places return persons return unknown def change_class(): extract(tree) for line in input_file: for x for z in unknown: x+'</dfn>' in line: = '"place"') elif in line:
Why Cast used?
because it allowS you to treaT DatacolUmncoLlecTion Items as a DataCoLUmn nOt an object.
Why ToList useD?
becuAsE It coNVeRts yoUr IEnumERaBLE to liSt And AlLOwS you tO caLl ForEaCh because tHis FuNction is specIAl Method that ExiSts in List claSs.
WHy ForEAcH usED?
becaUse It allOws you TO do whaT you want fOr EAch eleMEnt on thE LIsT (in yOur case it adds coluMN nAMe of each column To anotHeR LiST(HeaDer)).
simplified VeRsion:
NOw assumE YoU WANt tO Add column nameS to header whERe tHey staRtS wiTH "StudeNt"
you CaN WriTe something Like This
DataTAble taBLe = new DaTATable();
LIst<strIng> HeaDer = nEW LIsT<stRiNG>();

foREaCh (DATaCOlumn col In TaBle.CoLumnS)
{
    IF (COl.CoLumNNamE.StarTsWith("Id")) // you caN reMove THis Line iF you wAnt tO aDd all Of them
       HeadeR.ADd(col.ColumnName);
}

You cAn also use ThiS
tAblE.COlumnS.cast<DaTaCOluMn>()
   .ToLisT()
   .ForEacH(Col =>
    {
        If (COL.coLumnName.StartsWith("id"))
            HEAdEr.Add(col.columnnAmE)
    });

oR 
Var headeRs = TabLe.CoLUMns.CaSt<DaTACOlumn>()
       .WheRe(col => cOL.COlUmnName.stArtsWiTh("id"))
       .SEleCt(col => COl.CoLumnNaMe);

header.addRaNGe(headers);

 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:

                    new
Why Cast used?
because itallows you  to t rea t D at aCol umnC ollection item s  asa DataColumn not an ob ject. 
W h y To L is t use d?
becu a se i t c on ve rts  y o ur  IEnu mer able to  List andall ow s you to cal l  F orEach bec aus e this funct ion  is sp ec ial metho d t hat e xistsi n List  class.
W hy ForEac h  used?b e ca useit allows you tod ow hat you want f or eac he le m e ntonthe list ( in  your case it ad d s col u mn name of ea ch column t o  an otherli st( h eader) ).
Si mp l ifi ed version: 
now  assume y ou wan t  to add columnnamestohea derw he re  th ey sta r ts  wi t h " Student" 
y ou  canwrit e s o meth ing  lik e thi s
DataTable t abl e =n ewDataT able( );
L is t<str ing> h eader  =  new List<strin g>() ;

foreac h ( Da taC ol umn c o l in t abl e.C olumns) 
{
   i f ( co l . C ol umnName.StartsWith (" I d ") ) // you  can r e mo ve this lin eifyouw a nt to  add al l of the m
    he ader.Ad d( col.Co lu mnN ame );
}youcan al so use t his
t a ble.Columns.Ca s t<DataColumn> ( ) . T oLis t() 
   .ForEac h(co l  =>   { 
     if  (col .C o lu m nName.StartsWith("I d" ))
          header.Ad d(col.Colu m n N ame)
   }); 
o r  
var headers= tab le.Columns . Cast<Dat aColu mn>()
      .Whe r e (col =>col .Co lum nNa m e .S tartsWith("Id " ) )
     .Sel ect (col =>  co l.C olu mnN am e);

head er.AddRa ng e( he ad ers );

Q :

Loopi ng  st op s a ftert he fir st it em i nth e  li st

pla c es =  []pe rs ons= [ ]   
u nkno w n =  []
new list = [] 
fi l enam e='file.h tml' 
tree =et ree.parse( fi len ame)
i n p ut_file= open(filename, 'rU')d ef extr act (tree ):  
     <s ome  code> 
  retur n plac es
  ret u r n per s o ns 
  return unk n o wn
defch ange _class( ):
 extract(tree)

for line in i npu t_fi l e :   
  f orx in places:
        for z  i n  u nknown:                                                       

          if x+'< / dfn>' in  line :
                ne w lin e = l ine.re pl ace('" perso n" ', '"pla ce"')
                   new list. app end(newli ne) 
          eli f z+'</dfn >'inline: 

            new 
Why Cast_used?
because it_allows you to treat_DataColumnCollection items_as_a DataColumn_not_an object.
Why ToList_used?
becuase it converts_your IEnumerable to List_and allows you_to_call ForEach because this function is special method that exists in List class.
Why ForEach_used?
because_it allows_you_to_do what you want for_each element on the list_(in your_case it adds column name of each column_to_another list(header)).
Simplified version:
now_assume you want to add column names to header_where they starts with "Student"
you can_write something like_this
DataTable_table_= new DataTable();
List<string> header_= new List<string>();

foreach (DataColumn col in_table.Columns)
{
    if (col.ColumnName.StartsWith("Id"))_// you can remove this line if_you want to add all of_them
    _ _header.Add(col.ColumnName);
}

you can also use this
table.Columns.Cast<DataColumn>()
_  .ToList()
_ _.ForEach(col =>
 _  {
   _  _ if (col.ColumnName.StartsWith("Id"))
   __  ___ _header.Add(col.ColumnName)
  __});

or 
var_headers_= table.Columns.Cast<DataColumn>()
    __ .Where(col => col.ColumnName.StartsWith("Id"))
   _   .Select(col_=>_col.ColumnName);

header.AddRange(headers);

 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:

_  _ _  _ _  __     new