A question about testing proper nesting of XML tags:
I got a list of tags, extracted from top to bottom from an xml file:

Closing tags are clearly indicated by forward slash
/to and /lastname tags are incorrectly nested. They should be switched. /lastname should be within to, /to parent tags.

tag_list = ['note', 'to', 'firstname', '/firstname', 'lastname', '/to', '/lastname', '/note']
What would be the code or direction to spot that /lastname tag is outside of its parent which is to, /to pair?
Cheers.
Make an empty stack.

Iterating through the list: 

if you find a start tag, push it onto the stack.
if you find an end tag, compare it to the entry on top of the stack.

if the stack is empty or the top doesn't match, fail.
if it matches, pop the stack and continue.


At the end of the iteration:

if the stack is empty, declare success.
otherwise fail.
Detecting incorrect nesting of XML tags in Python