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>Your code is <strong>nearly correct</strong>, you only need to replace all appearance of <code>character</code> in the inner loop with <code>word</code>; <code>word</code> was never used in that inner loop:</p>

<pre><code>        ...
        for index, word in enumerate(htmlList):
            if index &gt; iterate:
                if word == '&gt;':   # here
                    tagList.append(tagAppend)
                    break
                tagAppend += word # here
        ...
</code></pre>

<hr>

<p>You could do without <code>enumerate</code> and a nested for loop with the following:</p>

<pre><code>def get_tag(htmlList):
    tag_list = []
    for x in htmlList:
        if x == '&lt;':
            tag = ''
            continue
        elif x == '&gt;':
            tag_list.append(tag)
            continue
        tag += x
    return tag_list
</code></pre>

python html list if-statement for-loop