Why is are the dicts in this list of dicts empty?
<p>As the title implies; I'm unsure as to why the dictionaries in this list of dictionaries are empty. I print the dictionaries out before I append them to the list and they all have 4 keys/values. </p>

<p>Please ignore the 'scrappiness' of the code- I always go through a process of writing it out basically and then refining! </p>

<p>Code:</p>

<pre><code>import ntpath, sys, tkFileDialog, Tkinter
import xml.etree.ElementTree as ET

class Comparison:
    def __init__(self, file):
        self.file = ET.parse(file)
        self.filename = self.get_file_name(file)
        self.root = self.file.getroot()
        self.file_length = len(self.root)
        self.data_dict = dict()
        self.master_list = list()
        self.parse_xml(self.root)
        print self.master_list

    def get_file_name(self, file):
        filename_list = list()
        for char in ntpath.basename(str(file)):
            filename_list.append(char)
            if ''.join(filename_list[-4:]) == '.xml':
                return ''.join(filename_list)

    def parse_xml(self, tree):
        for child in tree:
            if tree == self.root:
                self.step_number = child.attrib['id']
            self.data_dict['Step'] = self.step_number
            if len(child.tag) &gt; 0:
                self.data_dict['Tag'] = child.tag
            else:
                self.data_dict['Tag'] = ""
            if len(child.attrib) &gt; 0:
                self.data_dict['Attrib'] = child.attrib
            else:
                self.data_dict['Attrib'] = ""
            if child.text is not None:
                self.data_dict['Text'] = child.text
            else:
               self.data_dict['Text'] = ""
            print self.data_dict
            print "Step: "+str(self.data_dict['Step'])
            try:
                print "Tag: "+str(self.data_dict['Tag'])
            except:
                pass
            try:
                for key,value in self.data_dict['Attrib'].iteritems():
                    print "Attrib: "+str(key)
                    print "Attrib Value: "+str(value)
            except:
                pass
            try:
                if len(str(self.data_dict['Text'])) &gt; 0:
                    print "Text Length: "+str(len(str(self.data_dict['Text'])))
                    print "Text: "+str(self.data_dict['Text'])
            except:
                pass
            print ""
            print len(self.data_dict)
            self.master_list.append(self.data_dict)
            print self.data_dict
            self.data_dict.clear()
            if len(child) &gt; 0:
                self.parse_xml(child)

if __name__ == "__main__":
    root = Tkinter.Tk()
    root.iconify()
    if sys.argv[1] != "":
        file_a = Comparison(sys.argv[1])
    else:
        file_a = Comparison(tkFileDialog.askopenfilename())
</code></pre>

<p>I presume you mean this:</p>

<pre><code> print len(self.data_dict)
 self.master_list.append(self.data_dict)
 print self.data_dict
 self.data_dict.clear()
</code></pre>

<p>The <code>dict</code> is empty because <em>you clear it</em>. Everything is a reference in Python.</p>

<pre><code>&gt;&gt;&gt; d = {k:v for k,v in zip(range(5),'abcde')}
&gt;&gt;&gt; id(d)
140199719344392
&gt;&gt;&gt; some_list = []
&gt;&gt;&gt; some_list.append(d)
&gt;&gt;&gt; some_list
[{0: 'a', 1: 'b', 2: 'c', 3: 'd', 4: 'e'}]
&gt;&gt;&gt; id(some_list[0])
140199719344392
&gt;&gt;&gt; d.clear()
&gt;&gt;&gt; some_list
[{}]
&gt;&gt;&gt; 
</code></pre>

<p>If you want a copy to be appended, then you need to explicitely copy it. If a shallow copy will do, then simply use <code>your_dict.copy()</code>:</p>

<pre><code>&gt;&gt;&gt; d = {k:v for k,v in zip(range(5),'abcde')}
&gt;&gt;&gt; d
{0: 'a', 1: 'b', 2: 'c', 3: 'd', 4: 'e'}
&gt;&gt;&gt; id(d)
140199686153480
&gt;&gt;&gt; some_list = []
&gt;&gt;&gt; some_list.append(d.copy())
&gt;&gt;&gt; id(some_list[0])
140199719344392
&gt;&gt;&gt; d.clear()
&gt;&gt;&gt; some_list
[{0: 'a', 1: 'b', 2: 'c', 3: 'd', 4: 'e'}]
&gt;&gt;&gt; 
</code></pre>

python python-2.7