Python Loop that gets html tags returning empty list instead of tags
<p>So I'm trying to make a function that will go through a list of html tags in a list as characters and return the tags. An example would be it would go through a list like below</p>

<p>['&lt;', 'h', 't', 'm', 'l', '>', '&lt;', 'h', 'e', 'a', 'd', '>', '&lt;', 'm', 'e', 't', 'a', '>']</p>

<p>and return a list like this</p>

<p>[ 'html', 'head', 'meta' ]</p>

<p>However when I run the function below it returns an empty list []</p>

<pre><code>def getTag(htmlList):
    tagList=[]
    for iterate, character in enumerate(htmlList):
        tagAppend = ''
        if character=='&lt;':
            for index, word in enumerate(htmlList):
                if index&gt;iterate:
                    if character=='&gt;':
                        tagList.append(tagAppend)
                        break
                    tagAppend += character

    return tagList
</code></pre>

<p>The program seem make sense to me? It creates an empty list (tagList) then it iterates through the list(htmlList) like the first list I posted.</p>

<p>When iterating if it comes across a '&lt;' it then adds all characters above the index where it found the '&lt;' to a string called tagAppend. It then stops when it reaches a '>' which ends the tag. The tagAppend is then added to the tagList. It then clears tagList and redoes to the loop.</p>

<p>I'm going to assume this is just an exercise for the sake of learning. Python has much better tools for parsing HTML (<a href="https://www.crummy.com/software/BeautifulSoup/" rel="nofollow">https://www.crummy.com/software/BeautifulSoup/</a>) or strings (<a href="https://docs.python.org/2/library/re.html" rel="nofollow">https://docs.python.org/2/library/re.html</a>) in general.</p>

<pre><code>def getTag(htmlList):
    tagList=[]
    for iterate, character in enumerate(htmlList):
        tagAppend = ''
        if character=='&lt;':
            for index, word in enumerate(htmlList):
                if index&gt;iterate:
                    # use word here otherwise this will never be True
                    if word=='&gt;':
                        tagList.append(tagAppend)
                        break
                    # and here
                    tagAppend += word

    return tagList
</code></pre>

<p>The key bug is the use of character instead of word. I think it will work fine otherwise. Although it is inefficient.</p>

<p>We can also simplify. No need for nested for loops.</p>

<pre><code>def getTag(htmlList):
    tagList=[]
    tag = ""
    for character in htmlList:
        if character == "&lt;":
            tag = ""
        elif character == "&gt;":
            tagList.append(tag)
        else:
            tag.append(character)

    return tagList
</code></pre>

<p>The above has some serious issues depending on what constraints are on the input data. It might be instructive to think through it and see if you can find them.</p>

<p>We can also use built-ins like split and join to great affect as mentioned in the other answer.</p>

python html list if-statement for-loop