title: python append dictionary to list
body: <p>According to <a href="https://stackoverflow.com/questions/23724136/appending-a-dictionary-to-a-list-in-a-a-loop-python">this</a> post, I need to use <code>.copy()</code> on a dictionary, if I want to reference a dictionary which gets updated in a loop (instead of always referencing the same dictionary). However, in my code example below this doesn't seem to work:</p>
  
  <p><code>main.py</code>:</p>
  
  <pre><code>import collections
  import json
  
  nodes_list = ['donald', 'daisy', 'mickey', 'minnie']
  edges_list = [('donald', 'daisy', '3'), ('mickey', 'minnie', '3'), ('daisy', 'minnie', '2')]
  node_dict, edge_dict = collections.defaultdict(dict), collections.defaultdict(dict)
  ultimate_list = []
  
  
  for n in nodes_list:
      node_dict["data"]["id"] = str(n)
      ultimate_list.append(node_dict.copy())
  
  for e in edges_list:
      edge_dict["data"]["id"] = str(e[2])
      edge_dict["data"]["source"] = e[0]
      edge_dict["data"]["target"] = e[1]
      ultimate_list.append(edge_dict.copy())
  
  print(json.dumps(ultimate_list, indent=2))
  </code></pre>
  
  <p>As a result here I get the following:</p>
  
  <pre><code>[
    {
      "data": {
        "id": "minnie"
      }
    },
    {
      "data": {
        "id": "minnie"
      }
    },
    {
      "data": {
        "id": "minnie"
      }
    },
    {
      "data": {
        "id": "minnie"
      }
    },
    {
      "data": {
        "target": "minnie",
        "id": "2",
        "source": "daysi"
      }
    },
    {
      "data": {
        "target": "minnie",
        "id": "2",
        "source": "daysi"
      }
    },
    {
      "data": {
        "target": "minnie",
        "id": "2",
        "source": "daysi"
      }
    }
  ]
  </code></pre>
  
  <p>Whereas I would actually expect to get this:</p>
  
  <pre><code>[
    {
      "data": {
        "id": "donald"
      }
    },
    {
      "data": {
        "id": "daisy"
      }
    },
    {
      "data": {
        "id": "mickey"
      }
    },
    {
      "data": {
        "id": "minnie"
      }
    },
    {
      "data": {
        "target": "donald",
        "id": "3",
        "source": "daysi"
      }
    },
    {
      "data": {
        "target": "mickey",
        "id": "3",
        "source": "minnie"
      }
    },
    {
      "data": {
        "target": "minnie",
        "id": "2",
        "source": "daysi"
      }
    }
  ]
  </code></pre>
  
  <p>Can anyone please tell me what I'm doing wrong here?</p>