{"id":3553,"date":"2021-04-26T09:21:41","date_gmt":"2021-04-26T03:51:41","guid":{"rendered":"https:\/\/python-programs.com\/?p=3553"},"modified":"2021-11-22T18:43:06","modified_gmt":"2021-11-22T13:13:06","slug":"python-how-to-copy-a-dictionary-shallow-copy-vs-deep-copy","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-how-to-copy-a-dictionary-shallow-copy-vs-deep-copy\/","title":{"rendered":"Python : How to copy a dictionary | Shallow Copy vs Deep Copy"},"content":{"rendered":"
Dictionaries are Python’s implementation of an associative array data structure. A dictionary is a grouping of key-value pairs. Each key pair is represented by a key pair and its associated value.<\/p>\n
A dictionary is defined by a list of key value pairs enclosed in curly braces and separated by a comma. The value of each key is separated by the column \u2018:’.<\/p>\n
It is not possible to sort a dictionary solely to obtain a representation of the sorted dictionary. Dictionary entries are ordered by default, but other data types, such as lists and tuples, are not. As a result, you’ll need an ordered data form like a list\u2014most likely a list of tuples.<\/p>\n
Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n There are several ways to copy a dictionary some of them are:<\/p>\n This technique involves traversing the entire dictionary and copying each and every element pointed by the key to a previously declared new dictionary.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n We can simply copy a dictionary using = operator.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n A shallow copy is when a new dictionary object is formed and references to existing dictionary objects are inserted into it.<\/p>\n In Python, the dictionary copy()<\/strong> method creates a shallow copy of the given dictionary. It’s identical to what we saw before when copying elements by traversing through a dictionary.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n In Python, the deepcopy() method is part of the copy module. It creates a new dictionary from the passed dictionary’s elements. This method recursively copies all of the elements of the given dictionary.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n Dictionaries are Python’s implementation of an associative array data structure. A dictionary is a grouping of key-value pairs. Each key pair is represented by a key pair and its associated value. A dictionary is defined by a list of key value pairs enclosed in curly braces and separated by a comma. The value of each …<\/p>\ndictionary = {'this': 200, 'is':100, 'BTechGeeks':300}<\/pre>\n
Old dictionary : {'this': 200, 'is': 100, 'BTechGeeks': 300}\r\nNew dictionary : {'this': 200, 'is': 100, 'BTechGeeks': 300}<\/pre>\n
Copy a Dictionary:<\/h2>\n
\n
Method #1:Using loop and copying element by element<\/h3>\n
\n
# given dictionary\r\ndictionary = {'this': 200, 'is': 100, 'BTechGeeks': 300}\r\n# Take a empty dictionary\r\nnewdict = {}\r\n# Traverse the old dictionary\r\nfor key in dictionary:\r\n newdict[key] = dictionary[key]\r\n# print old dictionary\r\nprint(\"Old dictionary : \", dictionary)\r\n# print new dictionary\r\nprint(\"New dictionary : \", newdict)\r\n<\/pre>\n
Old dictionary : {'this': 200, 'is': 100, 'BTechGeeks': 300}\r\nNew dictionary : {'this': 200, 'is': 100, 'BTechGeeks': 300}<\/pre>\n
Method #2: Using = operator<\/h3>\n
# given dictionary\r\ndictionary = {'this': 200, 'is': 100, 'BTechGeeks': 300}\r\n# copying dictionary to new dictionary\r\nnewdict = dictionary\r\n# print old dictionary\r\nprint(\"Old dictionary : \", dictionary)\r\n# print new dictionary\r\nprint(\"New dictionary : \", newdict)\r\n<\/pre>\n
Old dictionary : {'this': 200, 'is': 100, 'BTechGeeks': 300}\r\nNew dictionary : {'this': 200, 'is': 100, 'BTechGeeks': 300}<\/pre>\n
\n
\nThe above code shows that when we update any(iterable or non-iterable) object in newdict, the same change is seen in dictionary.<\/li>\n<\/ul>\nMethod #3: Using copy() to create a shallow copy of dictionary<\/h3>\n
# given dictionary\r\ndictionary = {'this': 200, 'is': 100, 'BTechGeeks': 300}\r\n# copying dictionary to new dictionary using copy\r\nnewdict = dictionary.copy()\r\n# updating new dictionary\r\nnewdict['hello'] = 400\r\n# print old dictionary\r\nprint(\"Old dictionary : \", dictionary)\r\n# print new dictionary\r\nprint(\"New dictionary : \", newdict)\r\n<\/pre>\n
Old dictionary : {'this': 200, 'is': 100, 'BTechGeeks': 300}\r\nNew dictionary : {'this': 200, 'is': 100, 'BTechGeeks': 300, 'hello': 400}<\/pre>\n
\n
Method #4: Using copy.deepcopy()\u00a0 to copy a dictionary<\/h3>\n
# importing copy\r\nimport copy\r\n# given dictionary\r\ndictionary = {'this': 200, 'is': 100, 'BTechGeeks': 300}\r\n# copying dictionary to new dictionary using deepcopy\r\nnewdict = copy.deepcopy(dictionary)\r\n# updating new dictionary\r\nnewdict['hello'] = 400\r\n# print old dictionary\r\nprint(\"Old dictionary : \", dictionary)\r\n# print new dictionary\r\nprint(\"New dictionary : \", newdict)\r\n<\/pre>\n
Old dictionary : {'this': 200, 'is': 100, 'BTechGeeks': 300}\r\nNew dictionary : {'this': 200, 'is': 100, 'BTechGeeks': 300, 'hello': 400}<\/pre>\n
\n