CodeIgniter Forums
Problem with Dictionary Copies - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: General (https://forum.codeigniter.com/forumdisplay.php?fid=1)
+--- Forum: Lounge (https://forum.codeigniter.com/forumdisplay.php?fid=3)
+--- Thread: Problem with Dictionary Copies (/showthread.php?tid=88329)



Problem with Dictionary Copies - leusiam - 08-23-2023

Hello there,

I'm new to this and trying to make a clone of a dictionary so that I may make changes without harming the original, as I learnt from scaler's post. However, even after making the copy, the modifications I make appear to have an effect on both the copy and the original dictionary. This is my code:
Code:
original_dict = {'x': 1, 'y': 2, 'z': 3}
copy_dict = original_dict

copy_dict['x'] = 100

print("Original Dictionary:", original_dict)
print("Copy Dictionary:", copy_dict)

Surprisingly, both dictionaries exhibit the same 'x' value. I thought I'd made a separate duplicate, but it appears that modifications to one are affecting the other. How can I actually make a duplicate of the dictionary?


RE: Problem with Dictionary Copies - ozornick - 08-23-2023

Forum PHP for programmers) There may be no answer