|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "<small><small><i>\n", |
| 8 | + "All the IPython Notebooks in this lecture series by Dr. Milan Parmar are available @ **[GitHub](https://github.com/milaan9/02_Python_Datatypes/tree/main/005_Python_Dictionary_Methods)**\n", |
| 9 | + "</i></small></small>" |
| 10 | + ] |
| 11 | + }, |
| 12 | + { |
| 13 | + "cell_type": "markdown", |
| 14 | + "metadata": {}, |
| 15 | + "source": [ |
| 16 | + "# Python Dictionary `copy()`\n", |
| 17 | + "\n", |
| 18 | + "The **`copy()`** method returns a shallow copy of the dictionary.\n", |
| 19 | + "\n", |
| 20 | + "**Syntax**:\n", |
| 21 | + "\n", |
| 22 | + "```python\n", |
| 23 | + "dict.copy()\n", |
| 24 | + "```" |
| 25 | + ] |
| 26 | + }, |
| 27 | + { |
| 28 | + "cell_type": "markdown", |
| 29 | + "metadata": {}, |
| 30 | + "source": [ |
| 31 | + "## `copy()` Parameters\n", |
| 32 | + "\n", |
| 33 | + "The **`copy()`** method doesn't take any parameters." |
| 34 | + ] |
| 35 | + }, |
| 36 | + { |
| 37 | + "cell_type": "markdown", |
| 38 | + "metadata": {}, |
| 39 | + "source": [ |
| 40 | + "## Return Value from `copy()`\n", |
| 41 | + "\n", |
| 42 | + "The **`copy()`** method method returns a shallow copy of the dictionary. It doesn't modify the original dictionary." |
| 43 | + ] |
| 44 | + }, |
| 45 | + { |
| 46 | + "cell_type": "code", |
| 47 | + "execution_count": 1, |
| 48 | + "metadata": { |
| 49 | + "ExecuteTime": { |
| 50 | + "end_time": "2021-06-09T18:09:50.137840Z", |
| 51 | + "start_time": "2021-06-09T18:09:50.119339Z" |
| 52 | + } |
| 53 | + }, |
| 54 | + "outputs": [ |
| 55 | + { |
| 56 | + "name": "stdout", |
| 57 | + "output_type": "stream", |
| 58 | + "text": [ |
| 59 | + "Orignal: {1: 'one', 2: 'two'}\n", |
| 60 | + "New: {1: 'one', 2: 'two'}\n" |
| 61 | + ] |
| 62 | + } |
| 63 | + ], |
| 64 | + "source": [ |
| 65 | + "# Example 1: How copy works for dictionaries?\n", |
| 66 | + "\n", |
| 67 | + "original = {1:'one', 2:'two'}\n", |
| 68 | + "new = original.copy()\n", |
| 69 | + "\n", |
| 70 | + "print('Orignal: ', original)\n", |
| 71 | + "print('New: ', new)" |
| 72 | + ] |
| 73 | + }, |
| 74 | + { |
| 75 | + "cell_type": "markdown", |
| 76 | + "metadata": {}, |
| 77 | + "source": [ |
| 78 | + "## Difference in Using `copy()` method, and `=` Operator to Copy Dictionaries\n", |
| 79 | + "\n", |
| 80 | + "When **`copy()`** method is used, a new dictionary is created which is filled with a copy of the references from the original dictionary.\n", |
| 81 | + "\n", |
| 82 | + "When **`=`** operator is used, a new reference to the original dictionary is created." |
| 83 | + ] |
| 84 | + }, |
| 85 | + { |
| 86 | + "cell_type": "code", |
| 87 | + "execution_count": 2, |
| 88 | + "metadata": { |
| 89 | + "ExecuteTime": { |
| 90 | + "end_time": "2021-06-09T18:09:52.023653Z", |
| 91 | + "start_time": "2021-06-09T18:09:52.010019Z" |
| 92 | + } |
| 93 | + }, |
| 94 | + "outputs": [ |
| 95 | + { |
| 96 | + "name": "stdout", |
| 97 | + "output_type": "stream", |
| 98 | + "text": [ |
| 99 | + "new: {}\n", |
| 100 | + "original: {}\n" |
| 101 | + ] |
| 102 | + } |
| 103 | + ], |
| 104 | + "source": [ |
| 105 | + "# Example 2: Using = Operator to Copy Dictionaries\n", |
| 106 | + "\n", |
| 107 | + "original = {1:'one', 2:'two'}\n", |
| 108 | + "new = original\n", |
| 109 | + "\n", |
| 110 | + "# removing all elements from the list\n", |
| 111 | + "new.clear()\n", |
| 112 | + "\n", |
| 113 | + "print('new: ', new)\n", |
| 114 | + "print('original: ', original)" |
| 115 | + ] |
| 116 | + }, |
| 117 | + { |
| 118 | + "cell_type": "markdown", |
| 119 | + "metadata": {}, |
| 120 | + "source": [ |
| 121 | + "**Note:** Here, when **`new`** dictionary is cleared, **`original`** dictionary is also cleared." |
| 122 | + ] |
| 123 | + }, |
| 124 | + { |
| 125 | + "cell_type": "code", |
| 126 | + "execution_count": 3, |
| 127 | + "metadata": { |
| 128 | + "ExecuteTime": { |
| 129 | + "end_time": "2021-06-09T18:09:55.590777Z", |
| 130 | + "start_time": "2021-06-09T18:09:55.568376Z" |
| 131 | + } |
| 132 | + }, |
| 133 | + "outputs": [ |
| 134 | + { |
| 135 | + "name": "stdout", |
| 136 | + "output_type": "stream", |
| 137 | + "text": [ |
| 138 | + "new: {}\n", |
| 139 | + "original: {1: 'one', 2: 'two'}\n" |
| 140 | + ] |
| 141 | + } |
| 142 | + ], |
| 143 | + "source": [ |
| 144 | + "# Example 3: Using copy() to Copy Dictionaries\n", |
| 145 | + "\n", |
| 146 | + "original = {1:'one', 2:'two'}\n", |
| 147 | + "new = original.copy()\n", |
| 148 | + "\n", |
| 149 | + "# removing all elements from the list\n", |
| 150 | + "new.clear()\n", |
| 151 | + "\n", |
| 152 | + "print('new: ', new)\n", |
| 153 | + "print('original: ', original)" |
| 154 | + ] |
| 155 | + }, |
| 156 | + { |
| 157 | + "cell_type": "markdown", |
| 158 | + "metadata": {}, |
| 159 | + "source": [ |
| 160 | + "**Note:** Here, when **`new`** dictionary is cleared, **`original`** dictionary remains unchanged." |
| 161 | + ] |
| 162 | + }, |
| 163 | + { |
| 164 | + "cell_type": "code", |
| 165 | + "execution_count": null, |
| 166 | + "metadata": {}, |
| 167 | + "outputs": [], |
| 168 | + "source": [] |
| 169 | + } |
| 170 | + ], |
| 171 | + "metadata": { |
| 172 | + "hide_input": false, |
| 173 | + "kernelspec": { |
| 174 | + "display_name": "Python 3", |
| 175 | + "language": "python", |
| 176 | + "name": "python3" |
| 177 | + }, |
| 178 | + "language_info": { |
| 179 | + "codemirror_mode": { |
| 180 | + "name": "ipython", |
| 181 | + "version": 3 |
| 182 | + }, |
| 183 | + "file_extension": ".py", |
| 184 | + "mimetype": "text/x-python", |
| 185 | + "name": "python", |
| 186 | + "nbconvert_exporter": "python", |
| 187 | + "pygments_lexer": "ipython3", |
| 188 | + "version": "3.8.8" |
| 189 | + }, |
| 190 | + "toc": { |
| 191 | + "base_numbering": 1, |
| 192 | + "nav_menu": {}, |
| 193 | + "number_sections": true, |
| 194 | + "sideBar": true, |
| 195 | + "skip_h1_title": false, |
| 196 | + "title_cell": "Table of Contents", |
| 197 | + "title_sidebar": "Contents", |
| 198 | + "toc_cell": false, |
| 199 | + "toc_position": {}, |
| 200 | + "toc_section_display": true, |
| 201 | + "toc_window_display": false |
| 202 | + }, |
| 203 | + "varInspector": { |
| 204 | + "cols": { |
| 205 | + "lenName": 16, |
| 206 | + "lenType": 16, |
| 207 | + "lenVar": 40 |
| 208 | + }, |
| 209 | + "kernels_config": { |
| 210 | + "python": { |
| 211 | + "delete_cmd_postfix": "", |
| 212 | + "delete_cmd_prefix": "del ", |
| 213 | + "library": "var_list.py", |
| 214 | + "varRefreshCmd": "print(var_dic_list())" |
| 215 | + }, |
| 216 | + "r": { |
| 217 | + "delete_cmd_postfix": ") ", |
| 218 | + "delete_cmd_prefix": "rm(", |
| 219 | + "library": "var_list.r", |
| 220 | + "varRefreshCmd": "cat(var_dic_list()) " |
| 221 | + } |
| 222 | + }, |
| 223 | + "types_to_exclude": [ |
| 224 | + "module", |
| 225 | + "function", |
| 226 | + "builtin_function_or_method", |
| 227 | + "instance", |
| 228 | + "_Feature" |
| 229 | + ], |
| 230 | + "window_display": false |
| 231 | + } |
| 232 | + }, |
| 233 | + "nbformat": 4, |
| 234 | + "nbformat_minor": 2 |
| 235 | +} |
0 commit comments