|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Python: how to Copy and Deep Copy Python Lists \n", |
| 8 | + "(c) Joe James 2023" |
| 9 | + ] |
| 10 | + }, |
| 11 | + { |
| 12 | + "cell_type": "markdown", |
| 13 | + "metadata": {}, |
| 14 | + "source": [ |
| 15 | + "### Assignment is not a Copy\n", |
| 16 | + "listA = listB does not create a copy. Changes to one list will be reflected in the other.\n", |
| 17 | + "listA and listB both reference the exact same list." |
| 18 | + ] |
| 19 | + }, |
| 20 | + { |
| 21 | + "cell_type": "code", |
| 22 | + "execution_count": 1, |
| 23 | + "metadata": {}, |
| 24 | + "outputs": [ |
| 25 | + { |
| 26 | + "name": "stdout", |
| 27 | + "output_type": "stream", |
| 28 | + "text": [ |
| 29 | + "[2, 44, 6, [1, 3]]\n", |
| 30 | + "140554034568968\n", |
| 31 | + "140554034568968\n" |
| 32 | + ] |
| 33 | + } |
| 34 | + ], |
| 35 | + "source": [ |
| 36 | + "listA = [2, 4, 6, [1, 3]]\n", |
| 37 | + "listB = listA\n", |
| 38 | + "listB[1] = 44\n", |
| 39 | + "print(listA)\n", |
| 40 | + "print(id(listA))\n", |
| 41 | + "print(id(listB))" |
| 42 | + ] |
| 43 | + }, |
| 44 | + { |
| 45 | + "cell_type": "markdown", |
| 46 | + "metadata": {}, |
| 47 | + "source": [ |
| 48 | + "### Shallow copy using the list() constructor\n", |
| 49 | + "Shallow copy only works for 1D lists of native data types. \n", |
| 50 | + "Sublists, dicts, and other objects will retain the same referece to those objects." |
| 51 | + ] |
| 52 | + }, |
| 53 | + { |
| 54 | + "cell_type": "code", |
| 55 | + "execution_count": 2, |
| 56 | + "metadata": {}, |
| 57 | + "outputs": [ |
| 58 | + { |
| 59 | + "name": "stdout", |
| 60 | + "output_type": "stream", |
| 61 | + "text": [ |
| 62 | + "[2, 4, 6, [55, 3]]\n" |
| 63 | + ] |
| 64 | + } |
| 65 | + ], |
| 66 | + "source": [ |
| 67 | + "listA = [2, 4, 6, [1, 3]]\n", |
| 68 | + "listB = list(listA)\n", |
| 69 | + "listB[1] = 44\n", |
| 70 | + "listB[3][0] = 55\n", |
| 71 | + "print(listA)" |
| 72 | + ] |
| 73 | + }, |
| 74 | + { |
| 75 | + "cell_type": "markdown", |
| 76 | + "metadata": {}, |
| 77 | + "source": [ |
| 78 | + "### Other ways to make a Shallow copy\n", |
| 79 | + "List comprehensions, list.copy(), or copy.copy() can also be used to make *shallow* copies" |
| 80 | + ] |
| 81 | + }, |
| 82 | + { |
| 83 | + "cell_type": "code", |
| 84 | + "execution_count": 3, |
| 85 | + "metadata": {}, |
| 86 | + "outputs": [ |
| 87 | + { |
| 88 | + "name": "stdout", |
| 89 | + "output_type": "stream", |
| 90 | + "text": [ |
| 91 | + "[2, 4, 6, [55, 3]]\n" |
| 92 | + ] |
| 93 | + } |
| 94 | + ], |
| 95 | + "source": [ |
| 96 | + "listA = [2, 4, 6, [1, 3]]\n", |
| 97 | + "listB = [x for x in listA]\n", |
| 98 | + "listB[1] = 44\n", |
| 99 | + "listB[3][0] = 55\n", |
| 100 | + "print(listA)" |
| 101 | + ] |
| 102 | + }, |
| 103 | + { |
| 104 | + "cell_type": "code", |
| 105 | + "execution_count": 4, |
| 106 | + "metadata": {}, |
| 107 | + "outputs": [ |
| 108 | + { |
| 109 | + "name": "stdout", |
| 110 | + "output_type": "stream", |
| 111 | + "text": [ |
| 112 | + "[2, 4, 6, [55, 3]]\n" |
| 113 | + ] |
| 114 | + } |
| 115 | + ], |
| 116 | + "source": [ |
| 117 | + "listA = [2, 4, 6, [1, 3]]\n", |
| 118 | + "listB = listA.copy()\n", |
| 119 | + "listB[1] = 44\n", |
| 120 | + "listB[3][0] = 55\n", |
| 121 | + "print(listA)" |
| 122 | + ] |
| 123 | + }, |
| 124 | + { |
| 125 | + "cell_type": "code", |
| 126 | + "execution_count": 5, |
| 127 | + "metadata": {}, |
| 128 | + "outputs": [ |
| 129 | + { |
| 130 | + "name": "stdout", |
| 131 | + "output_type": "stream", |
| 132 | + "text": [ |
| 133 | + "[2, 4, 6, [55, 3]]\n" |
| 134 | + ] |
| 135 | + } |
| 136 | + ], |
| 137 | + "source": [ |
| 138 | + "import copy\n", |
| 139 | + "listA = [2, 4, 6, [1, 3]]\n", |
| 140 | + "listB = copy.copy(listA)\n", |
| 141 | + "listB[1] = 44\n", |
| 142 | + "listB[3][0] = 55\n", |
| 143 | + "print(listA)" |
| 144 | + ] |
| 145 | + }, |
| 146 | + { |
| 147 | + "cell_type": "markdown", |
| 148 | + "metadata": {}, |
| 149 | + "source": [ |
| 150 | + "### How to Deep Copy a Python List\n", |
| 151 | + "use copy.deepcopy()" |
| 152 | + ] |
| 153 | + }, |
| 154 | + { |
| 155 | + "cell_type": "code", |
| 156 | + "execution_count": 6, |
| 157 | + "metadata": {}, |
| 158 | + "outputs": [ |
| 159 | + { |
| 160 | + "name": "stdout", |
| 161 | + "output_type": "stream", |
| 162 | + "text": [ |
| 163 | + "[2, 4, 6, [1, 3]]\n" |
| 164 | + ] |
| 165 | + } |
| 166 | + ], |
| 167 | + "source": [ |
| 168 | + "import copy\n", |
| 169 | + "listA = [2, 4, 6, [1, 3]]\n", |
| 170 | + "listB = copy.deepcopy(listA)\n", |
| 171 | + "listB[1] = 44\n", |
| 172 | + "listB[3][0] = 55\n", |
| 173 | + "print(listA)" |
| 174 | + ] |
| 175 | + }, |
| 176 | + { |
| 177 | + "cell_type": "markdown", |
| 178 | + "metadata": {}, |
| 179 | + "source": [ |
| 180 | + "### Deepcopy with Objects" |
| 181 | + ] |
| 182 | + }, |
| 183 | + { |
| 184 | + "cell_type": "code", |
| 185 | + "execution_count": 7, |
| 186 | + "metadata": {}, |
| 187 | + "outputs": [ |
| 188 | + { |
| 189 | + "name": "stdout", |
| 190 | + "output_type": "stream", |
| 191 | + "text": [ |
| 192 | + "140554035637216 140554035637104\n", |
| 193 | + "140554035637216 140554035637216\n", |
| 194 | + "140554035637216 140554035637048\n" |
| 195 | + ] |
| 196 | + } |
| 197 | + ], |
| 198 | + "source": [ |
| 199 | + "class Pony():\n", |
| 200 | + " def __init__(self, n):\n", |
| 201 | + " self.name = n\n", |
| 202 | + " \n", |
| 203 | + "# copy.copy on an object gives you 2 unique objects (with same attributes)\n", |
| 204 | + "pony1 = Pony('Pinto')\n", |
| 205 | + "pony2 = copy.copy(pony1)\n", |
| 206 | + "print(id(pony1), id(pony2))\n", |
| 207 | + "\n", |
| 208 | + "# copy.copy on a list of objects gives you 2 unique lists containing the exact same objects \n", |
| 209 | + "# (ie. new list is a shallow copy)\n", |
| 210 | + "m = [pony1, pony2]\n", |
| 211 | + "n = copy.copy (m)\n", |
| 212 | + "print(id(m[0]), id(n[0]))\n", |
| 213 | + "\n", |
| 214 | + "# use copy.deepcopy to deep copy a list of objects\n", |
| 215 | + "n = copy.deepcopy (m)\n", |
| 216 | + "print(id(m[0]), id(n[0]))" |
| 217 | + ] |
| 218 | + }, |
| 219 | + { |
| 220 | + "cell_type": "code", |
| 221 | + "execution_count": null, |
| 222 | + "metadata": {}, |
| 223 | + "outputs": [], |
| 224 | + "source": [] |
| 225 | + } |
| 226 | + ], |
| 227 | + "metadata": { |
| 228 | + "kernelspec": { |
| 229 | + "display_name": "Python 3", |
| 230 | + "language": "python", |
| 231 | + "name": "python3" |
| 232 | + }, |
| 233 | + "language_info": { |
| 234 | + "codemirror_mode": { |
| 235 | + "name": "ipython", |
| 236 | + "version": 3 |
| 237 | + }, |
| 238 | + "file_extension": ".py", |
| 239 | + "mimetype": "text/x-python", |
| 240 | + "name": "python", |
| 241 | + "nbconvert_exporter": "python", |
| 242 | + "pygments_lexer": "ipython3", |
| 243 | + "version": "3.7.0" |
| 244 | + } |
| 245 | + }, |
| 246 | + "nbformat": 4, |
| 247 | + "nbformat_minor": 2 |
| 248 | +} |
0 commit comments