user97662
user97662

Reputation: 970

create a copy of an existing dictionary but assign a unique ID for the copy

I want to create a copy of a existing dict

b = {'name':'someone'}
copy_b = b

if do this, changes made in copy_b will affect b, how can I make copy_b unique to b?

Thanks,

Upvotes: 2

Views: 52

Answers (2)

Aswin Murugesh
Aswin Murugesh

Reputation: 11070

use dict()

>>>a = {1:1}
>>>b = dict(a)
>>>b[2] = 2
>>>a
{1:1}

This will do good

Upvotes: 1

Joran Beasley
Joran Beasley

Reputation: 114038

from copy import deepcopy

copy_b = deepcopy(d)

should work ... I would think at least

if its just a simple dictionary like above

copy_b = dict(d)

Upvotes: 1

Related Questions