Dictionary key and value flipping themselves unexpectedly
Question: I am running python 3.5, and I've defined a function that creates XML
SubElements and adds them under another element. The attributes are in a
dictionary, but for some reason the dictionary keys and values will sometimes
flip when I execute the script.

Here is a snippet of kind of what I have (the code is broken into many
functions so I combined it here)

    
    
    import xml.etree.ElementTree as ElementTree
    
    def AddSubElement(parent, tag, text='', attributes = None):
        XMLelement = ElementTree.SubElement(parent, tag)
        XMLelement.text = text
        if attributes != None:
            for key, value in attributes:
                XMLelement.set(key, value)
        print("attributes =",attributes)
        return XMLelement
    
    descriptionTags = ([('xmlns:g' , 'http://base.google.com/ns/1.0')])
    XMLroot = ElementTree.Element('rss')
    XMLroot.set('version', '2.0')
    XMLchannel = ElementTree.SubElement(XMLroot,'channel')
    AddSubElement(XMLchannel,'g:description', 'sporting goods', attributes=descriptionTags )
    AddSubElement(XMLchannel,'link', 'http://'+ domain +'/')
    XMLitem = AddSubElement(XMLchannel,'item')  
    AddSubElement(XMLitem, 'g:brand', Product['ProductManufacturer'], attributes=bindingParam)
    AddSubElement(XMLitem, 'g:description', Product['ProductDescriptionShort'], attributes=bindingParam)
    AddSubElement(XMLitem, 'g:price', Product['ProductPrice'] + ' USD', attributes=bindingParam)
    

The key and value does get switched! Because I'll see this in the console
sometimes:

    
    
    attributes = [{'xmlns:g', 'http://base.google.com/ns/1.0'}]
    attributes = [{'http://base.google.com/ns/1.0', 'xmlns:g'}]
    attributes = [{'http://base.google.com/ns/1.0', 'xmlns:g'}]
    ...
    

And here is the xml string that sometimes comes out:

    
    
    <rss version="2.0">
    <channel>
        <title>example.com</title>
        <g:description xmlns:g="http://base.google.com/ns/1.0">sporting goods</g:description>
        <link>http://www.example.com/</link>
        <item>
            <g:id http://base.google.com/ns/1.0="xmlns:g">8987983</g:id>
            <title>Some cool product</title>
            <g:brand http://base.google.com/ns/1.0="xmlns:g">Cool</g:brand>
            <g:description http://base.google.com/ns/1.0="xmlns:g">Why is this so cool?</g:description>
            <g:price http://base.google.com/ns/1.0="xmlns:g">69.00 USD</g:price>
            ...
    

What is causing this to flip?


Answer: 
    attributes = [{'xmlns:g', 'http://base.google.com/ns/1.0'}]
    

This is a list containing a set, not a dictionary. Neither sets nor
dictionaries are ordered.

