Python copying or cloning a defaultdict variable

How can I make a duplicate copy (not just assigning a new pointer to the same location in memory) of Python's defaultdict object?

from collections import defaultdict
itemsChosen = defaultdict(list)    
itemsChosen[1].append(1)
dupChosen = itemsChosen
itemsChosen[2].append(1)
print dupChosen

What the code above does is a shallow copy and returns

defaultdict(<type 'list'>, {1: [1], 2: [1]})

whereas what I'm looking for it to return

defaultdict(<type 'list'>, {1: [1]})

Thanks.

Answers


Use copy:

from copy import copy

dupChosen = copy(itemsChosen)

In case of multiple nestings there is also deepcopy.


Need Your Help

Replace T.co with full twitter link using API

php regex twitter

I am currently using the EpiTwitter API to grab Tweets and store them in the database however I want to replace any 'T.co' links in a given tweet and replace them with the full links. The regex is ...

rails, rack-cors, nginx, angualrjs and cors

ruby-on-rails nginx cors rails-api rack-cors

Still getting "Origin MYSITE.com is not allowed by Access-Control-Allow-Origin." errors.