{"id":25040,"date":"2021-11-10T09:37:07","date_gmt":"2021-11-10T04:07:07","guid":{"rendered":"https:\/\/python-programs.com\/?p=25040"},"modified":"2021-11-10T09:37:07","modified_gmt":"2021-11-10T04:07:07","slug":"python-dictionary-copy-function-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-dictionary-copy-function-with-examples\/","title":{"rendered":"Python Dictionary copy() Function with Examples"},"content":{"rendered":"
Dictionary copy() Function in Python:<\/strong><\/p>\n The copy() method makes a duplicate of the specified dictionary(copy).<\/p>\n Syntax:<\/strong><\/p>\n Parameters:<\/strong> This method doesn’t accept any parameters<\/p>\n Return Value:<\/strong><\/p>\n This method gives you a shallow copy of the dictionary. It makes no changes to the original dictionary.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n When the copy() method is used, a new dictionary is created that contains a copy of the original dictionary’s references. Approach:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n <\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" Dictionary copy() Function in Python: The copy() method makes a duplicate of the specified dictionary(copy). Syntax: dictionary.copy() Parameters: This method doesn’t accept any parameters Return Value: This method gives you a shallow copy of the dictionary. It makes no changes to the original dictionary. Examples: Example1: Input: Given dictionary = {20: ‘good’, 30: ‘morning’, 40: …<\/p>\ndictionary.copy()<\/pre>\n
Given dictionary = {20: 'good', 30: 'morning', 40: 'btechgeeks'}<\/pre>\n
The given original dictionary is:\r\n{20: 'good', 30: 'morning', 40: 'btechgeeks'}\r\nThe copied new dictionary is:\r\n{20: 'good', 30: 'morning', 40: 'btechgeeks'}<\/pre>\n
Given dictionary = {'hello': 100, 'btechgeeks': 200}<\/pre>\n
The given original dictionary is:\r\n{'hello': 100, 'btechgeeks': 200}\r\nThe copied new dictionary is:\r\n{'hello': 100, 'btechgeeks': 200}<\/pre>\n
Dictionary copy() Function with Examples in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Give the dictionary as static input and store it in a variable.\r\ngvn_dict = {20: 'good', 30: 'morning', 40: 'btechgeeks'}\r\n# Copy the given dictionary into a new dictionary using the copy() function.\r\n# Store it in another variable.\r\nnew_dict = gvn_dict.copy()\r\n# Print the given original dictionary.\r\nprint(\"The given original dictionary is:\")\r\nprint(gvn_dict)\r\n# Print the copied new dictionary.\r\nprint(\"The copied new dictionary is:\")\r\nprint(new_dict)\r\n<\/pre>\n
The given original dictionary is:\r\n{20: 'good', 30: 'morning', 40: 'btechgeeks'}\r\nThe copied new dictionary is:\r\n{20: 'good', 30: 'morning', 40: 'btechgeeks'}<\/pre>\n
Difference between Dictionary copy() Function , = Operator<\/h5>\n
\nWhen the = operator is used, it creates a new reference to the original dictionary.<\/p>\n\n
# Give the dictionary as static input and store it in a variable.\r\ngvn_dict = {'hello': 100, 'btechgeeks': 200}\r\n# Copy the given dictionary into a new dictionary using the '=' Operator.\r\n# Store it in another variable.\r\nnew_dict = gvn_dict\r\n# Remove all the elements from the new dictionary using the clear() function.\r\nnew_dict.clear()\r\n# Print the given original dictionary.\r\nprint(\"The given original dictionary is:\")\r\nprint(gvn_dict)\r\n# Print the copied new dictionary.\r\nprint(\"The copied new dictionary is:\")\r\nprint(new_dict)\r\n<\/pre>\n
The given original dictionary is:\r\n{}\r\nThe copied new dictionary is:\r\n{}<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Take a dictionary and initialize it with an empty dictionary using dict() or {}.\r\ngvn_dict = {}\r\n# Give the number of keys as user input using int(input()) and store it in a variable.\r\nnumb_of_kys = int(\r\n input('Enter some random number of keys of the dictionary = '))\r\n# Loop till the given number of keys using for loop.\r\nfor p in range(numb_of_kys):\r\n # Inside the for loop scan the key and value as\r\n # user input using input(),split() functions\r\n # and store them in two separate variables.\r\n keyy, valuee = input(\r\n 'Enter key and value separated by spaces = ').split()\r\n # Initialize the key with the value of the dictionary.\r\n gvn_dict[keyy] = valuee\r\n\r\n# Copy the given dictionary into a new dictionary using the copy() function.\r\n# Store it in another variable.\r\nnew_dict = gvn_dict.copy()\r\n# Print the given original dictionary.\r\nprint(\"The given original dictionary is:\")\r\nprint(gvn_dict)\r\n# Print the copied new dictionary.\r\nprint(\"The copied new dictionary is:\")\r\nprint(new_dict)\r\n<\/pre>\n
Enter some random number of keys of the dictionary = 2\r\nEnter key and value separated by spaces = 100 hello\r\nEnter key and value separated by spaces = 200 btechgeeks\r\nThe given original dictionary is:\r\n{'100': 'hello', '200': 'btechgeeks'}\r\nThe copied new dictionary is:\r\n{'100': 'hello', '200': 'btechgeeks'}<\/pre>\n