Dictionary key and value flipping themselves unexpectedly
<p>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.</p>

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

<pre><code>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)
</code></pre>

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

<pre><code>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'}]
...
</code></pre>

<p>And here is the xml string that sometimes comes out:</p>

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

<p>What is causing this to flip?</p>

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

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

python xml