Python: Looping through a list of dictionaries and extracting values to a new dictionary
<p>I need to loop on a list of dictionaries and check if a value exist. If it exists then I take another value from this same dictionary and store it on a new dictionary inside another list. What I have is this</p>

<pre><code>class_copy=[]
for root, dirs, files in os.walk(files_path+"/TD"):
    for file in files:
        file_name=os.path.splitext(file)[0]
        for d in data_list:
            if d['id'] == file_name:
                cc['class']=d['fic']
                class_copy.append(cc)
                break
</code></pre>

<p>So I loop through some files. data_list is a list of dictionaries. These dictionaries each have an 'id' which matches a file name so when the dictionary 'd' with the value 'id' is found I take the value of 'fic' on dictionary 'd' and make a new dictionary with the key 'class' to store the value of 'fic'. Then I store this dictionary on a new list of dictionaries called class_copy. </p>

<p>The problem is that after looping through all, all the dictionaries in class_copy are the same. I guess that by looping and changing the instance of d the values on class_copy are also changing but how could I retain the values? Is there a better way to do this?</p>

<pre><code>class_copy.append({'class': d['fic']'})
</code></pre>

python loops dictionary